Janusz Dalecki Messages: 49 Registered: January 2010 Location: Sydney
Member
Hi all,
I have inherited some jax-rs code to maintain and develop and I am really puzzled by
how does the jax-rs really work.
I have read a few tutorials on jax-rs and I have tried to find out what those two annotations actually mean/do
@Consumes/@Produces.
Could somebody please explain (in a plain language) what they do?
My understanding is (probably far from the truth) that -
@Consumes({ MediaType.APPLICATION_JSON})
means that the jax-rx (jersey) will convert objects passed by any browser into my app as JSON objects (name:value pairs stuff) but the function defined in the class handling the request is like this:
Am 30.11.2012 11:38, schrieb Janusz Dalecki:
> @Consumes({ MediaType.APPLICATION_JSON })
> @Produces({ MediaType.APPLICATION_JSON })
> public Product create(final Product prod) {
> prod.setId(null);
> return ...
> }
>
> so it is POJO object (Product), and it works !!! why???
> What am I missing???
Your assumption is correct.
@Consumes({ MediaType.APPLICATION_JSON }) instructs JAX-RS to accept
JSON input. It then searches for a @Provider that converts JSON into a
Product POJO.
@Produces is the other way around. It takes return values from methods
and uses a @Provider to convert POJOs to JSON.