Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Gyrex » @Consumes & @Produces(Explanation)
@Consumes & @Produces [message #988516] Fri, 30 November 2012 10:38 Go to next message
Janusz Dalecki is currently offline Janusz DaleckiFriend
Messages: 56
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:
@POST
      @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???
Regards,
Janusz
Re: @Consumes & @Produces [message #989371 is a reply to message #988516] Wed, 05 December 2012 21:20 Go to previous message
Gunnar Wagenknecht is currently offline Gunnar WagenknechtFriend
Messages: 453
Registered: July 2009
Location: Germany ✈ Vancouver
Senior Member

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.

-Gunnar

--
Gunnar Wagenknecht
gunnar@xxxxxxxx
http://wagenknecht.org/
Previous Topic:Gyrex node failed to be listed in cluster environment
Next Topic:Scope of the zookeeper support
Goto Forum:
  


Current Time: Fri Dec 19 08:54:51 GMT 2014

Powered by FUDForum. Page generated in 0.01604 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software