users@jersey.java.net

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

From: Martin Matula <martin.matula_at_oracle.com>
Date: Thu, 19 May 2011 23:14:57 +0200

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
> <mailto: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...
>
>