I have a REST service operation defined in a controller class, as shown:
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("create")
public Response createWidget(@BeanParam Widget widget) {
...
}
Widget is a POJO bean class, i.e. 2 private fields named foo
& bar
of type String
with public getters & setters, and a public no-arg constructor.
The POST request body is:
{ "foo": "Some text", "bar": "Some more text" }
and has header Content-Type: application/json
On firing this request, the createWidget
method gets a Widget
object as argument but both String
fields are null
.
Can someone tell me what else is needed for the fields to be populated? I think some annotations may be required in the POJO bean class. If the content type was application/x-www-form-urlencoded
, then I know that the fields should be annotated @FormParam
, but I'm not sure what the annotation should be for application/json
content.
Thanks a lot for your help... - Ajoy
Generally, with the exception of some forms annotations, the body parameter does not need any annotations. That is actually how JAX=RS will determine that it is the body. So you can only have one non-annotated parameter, as you can only have one body. So just change what you have to this (just removing the @BeanParam
. As long as you have a JSON provider, it should work.
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Path("create")
public Response createWidget(Widget widget) {