4

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Ajoy Bhatia
  • 603
  • 6
  • 30
  • 1
    The message body parameter should have _no_ annotation at all. If you remove the `@BeanParam`, and you get an error like "No MessageBodreadter for type Wdiget and application/json", then you neen a JSON provider – Paul Samsotha Dec 11 '15 at 19:02
  • Thanks, @peeskillet. That worked. I did not have to do anything else because the JacksonJaxbJsonProvider was found. If you convert your comment to an answer, I will accept it. – Ajoy Bhatia Dec 11 '15 at 22:01

1 Answers1

5

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) {
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720