2013-04-25 45 views
0

angularjs代碼:

$http({ 
    method: 'POST', 
    url:'/hello/rest/user/test', 
    data: {user: 'aaaaa'} 
}); 

服務器代碼:

@POST 
@Path("/test") 
public Response merge(@Context HttpServletRequest request) { 
    System.out.println(request.getParameter("user")); 
    if (request.getParameter("user") == null) { 
     return Response.status(Status.BAD_REQUEST).build(); 
    } 
    return Response.ok().build(); 
} 

的request.getParameter( 「用戶」)是一個空。我無法通過這種方式來修改參數。

回答

0

您需要查看正文而不是後期參數...

您可能沒有收到POST參數。

您正在收到一個類似於{「user」:「aaaaa」}的正文,您需要解析該JSON響應。

1

如果你想使用請求參數,你需要通過數據

$http.post('/hello/rest/user/test', { 
    user : 'aaaaa' 
    }, { 
     headers : { 
      "Content-Type" : 'application/x-www-form-urlencoded;charset=utf-8' 
     }, 
     transformRequest : [function(data) { 
      return angular.isObject(data) 
        ? jQuery.param(data) 
        : data; 
     }] 
    }); 

又讀this question

+0

應用程序/ x-WWW窗體-urlencoded,形式。但我只想使用'application/json'。我不想用'表格'。 – 2013-04-26 13:38:13

+0

如果你想發送數據作爲請求體,即JSON那麼你不能使用'request.getParameter()'。您需要使用'@ RequestBody'註釋以及jackson庫來訪問反序列化請求 – 2013-04-26 13:41:49

+0

@RequestBody,是的,它可以工作。謝謝。 – 2013-04-27 13:41:36