In javascript I am making an ajax post request to one of my JAX-RS functions like this
var postPackingListRequest = $http({
method: "post",
url: "/rest/v1/submit",
data: $scope.items
});
And now in my JAX-RS method I am trying to get the varibale $scope.items
that was passed. I know I can get path params in the path like this
public Response getPathParams(@QueryParam("id") int id) {
But how can I get the data, which is passed in the body?
Thanks
EDIT
@POST
@Path("submit")
@Consumes(MediaType.APPLICATION_JSON)
@ApiResponses({ @ApiResponse(code = 201, response = Response.class) })
@Produces("application/json")
public Response submitPackingList(TestUser testUser) {
}
public class TestUser {
public String id;
public String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
When I send a request to this with the TestUser there I am getting a HTTP 400 error. Here is how I am sending it
var postPackingListRequest = $http({
method: "post",
url: "/rest/v1/submit",
data: "{'user':'1', 'name':james}"
});
Lets say your method is supposed to handle GET request like below
@GET
public Response getPathParams(@QueryParam("id") int id)
Now in the above method every argument must be annotated with something. In your case it is QueryParam. It may be PathParam and several others as well. Only one argument is allowed which is not annoatated in JAX-RS. Let's say you have User object like below:
public class User{
String userName;
int userId;
//getters and setters
}
and you want to accept user details that are coming via body of request then your method signature would look like below:
@GET
public Response getPathParams(@QueryParam("id") int id, User user)
Depending upon what the above method consumes whether it be json or xml the request body will be converted to User object and will get bind to your argument. In case you are using Json you will need explicit MessageBodyReader for it.