users@jersey.java.net

[Jersey] Re: Application-Specific content types and JAXB annotations

From: Jason Erickson <jason_at_jasonerickson.com>
Date: Tue, 31 May 2011 08:03:15 -0700

Thanks. I haven't done it yet but I definitely had something similar in mind. Thanks for the pointer.

Sent from my iPhone

On May 31, 2011, at 3:06 AM, Jakub Podlesak <jakub.podlesak_at_oracle.com> wrote:

> FWIIW, I would also recommend to look at the javax.ws.rs.ext.Providers javadoc:
>
> http://jsr311.java.net/nonav/releases/1.1/javax/ws/rs/ext/Providers.html
>
> as when implementing your custom message body reader/writer for versioned media types,
> you can get an instance of Providers injected there (via @javax.ws.rs.core.Context)
> and then you can leverage existing message body readers/writers for the basic media types.
>
> ~Jakub
>
> On 05/20/2011 09:54 AM, Martin Matula wrote:
>>
>> Here is the javadoc:
>> http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/ext/MessageBodyReader.html
>> http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/ext/MessageBodyWriter.html
>> You can also look at this lab: http://download.oracle.com/javaone/javaone2009-hol-restwebservice.zip
>> solutions/exercise2 implements two message body writers (PropertyWriter and PropertyListWriter)
>> Martin
>>
>> On 19.5.2011 23:33, Jason Erickson wrote:
>>>
>>> Thanks! It didn't occur to me that that would work.
>>>
>>> I still might want a custom MessageBodyWriter/Reader for versioned media types, though, so I'm still interested in pointers to samples and/or documentation.
>>>
>>> On May 19, 2011, at 2:14 PM, Martin Matula wrote:
>>>
>>>> No need to write a custom messagebodyreader/writter. Anything "+json" should work out of the box - looks like you haven't tried?
>>>> Martin
>>>>
>>>> On 19.5.2011 22:05, Daniel Larsson wrote:
>>>>>
>>>>> You can write code like your first example, even with a custom mime type, but you'll need to provide a custom MessageBodyWriter for it. This should be pretty simple, as you can utilize the original MessageBodyWriter to do the actual work.
>>>>>
>>>>>
>>>>> 2011/5/19 Jason Erickson <jason_at_jasonerickson.com>
>>>>>
>>>>> Say I have a method like this:
>>>>>
>>>>> @GET
>>>>> @Produces("application/json")
>>>>> @Path("{id}")
>>>>> public Car getCar(@PathParam("id") Long id) {
>>>>> return carDao.getById(id);
>>>>> }
>>>>>
>>>>> This works great. However, I keep coming across recommendations not to use things like "application/json" when you really mean something custom like "application/vnd.car+json" or even "application/vnd.car+1.0+json". If I wanted to do something like that for my @Produces, then I would have to change it something like this right?
>>>>>
>>>>>
>>>>> @GET
>>>>> @Produces("application/vnd.car+1.0+json")
>>>>> @Path("{id}")
>>>>> public Response getCar(@PathParam("id") Long id) {
>>>>> Car car = carDao.getById(id);
>>>>> JSONObject entity = new JSONObject();
>>>>> ... some logic to convert car to entity ...
>>>>> return Response.ok().entity(entity).build();
>>>>> }
>>>>>
>>>>> Is this the recommended way? Is there a way to still return a Car object and have a custom content type? I still want to produce JSON, but I want to support vendor specific content negotiation. I have the same question on the @Consumes side of things, but one problem at a time...
>>>>>
>>>>
>>>
>>
>