Ajoy Bhatia Ajoy Bhatia - 9 months ago 67
JSON Question

Null fields in POJO with application/json and BeanParam

I have a REST service operation defined in a controller class, as shown:

public Response createWidget(@BeanParam Widget widget) {

Widget is a POJO bean class, i.e. 2 private fields named
of type
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
method gets a
object as argument but both
fields are

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
, then I know that the fields should be annotated
, but I'm not sure what the annotation should be for

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.

public Response createWidget(Widget widget) {