0

angularjs code:

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

Server code:

@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("user") is a null. I cannot revice a parameter by this way.

C_C
  • 11
  • 4

2 Answers2

1

If you want to use request parameter, you need to pass the data as

$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;
        }]
    });

Also read this question

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • application/x-www-form-urlencoded, form. But I just want to use 'application/json'. I don't want to use 'form'. – C_C Apr 26 '13 at 13:38
  • if you want to sent data as request body ie json then you cannot use `request.getParameter()`. You need to use `@RequestBody` annotation along with jackson library to access the deserialized request – Arun P Johny Apr 26 '13 at 13:41
0

You need to look at the body rather than for the post parameter...

You may not be receiving POST parameters.

You are receiving a body that looks like this {"user":"aaaaa"} and you need to parse that JSON response.

jkumar
  • 1