Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » 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: 63
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: 486
Registered: July 2009
Location: San Francisco ✈ Germany
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 Apr 26 15:51:32 GMT 2024

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

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

Back to the top