users@jersey.java.net

[Jersey] Application-Specific content types and JAXB annotations

From: Jason Erickson <jason_at_jasonerickson.com>
Date: Wed, 18 May 2011 18:03:20 -0700

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