JBossWiki : JAXRSContentNegotiation
Wiki Home: JAXRSContentNegotiation [EN]
[Permalink]
[Recent Changes]
[History]
[RSS Feed][RDF Feed][Atom Feed][Print]

JAX-RS Content Negotiation

The HTTP protocol has built in content negotiation headers that allow the client and server to specify what content they are transferring and what content they would prefer to get. The server declares content preferences via the @ProduceMime? and @ConsumeMime? headers.

@ConsumeMime?

@ConsumeMime? is an array of media types that a particular resource or resource method consumes. For example:

@ConsumeMime("text/*")
@Path("/library")
public class Library {

   @POST
   public String stringBook(String book) {...}


   @ConsumeMime("text/xml")
   @POST
   public String jaxbBook(Book book) {...}

When a client makes a request, JAX-RS first finds all methods that match the path, then, it sorts things based on the content-type header sent by the client. So, if a client sent:

POST /library
content-type: text/plain

thsi sis  anice book

The stringBook() method would be invoked because it matches to the default "text/*" media type. Now, if the client instead sends XML:

POST /library
content-type: text/xml

<book name="EJB 3.0" author="Bill Burke"/>

The jaxbBook() method would be invoked.

@ProduceMime?

The @ProduceMime? is used to map a client request and match it up to the client's Accept header. The Accept HTTP header is sent by the client and defines the media types the client prefers to receive from the server.

@ProduceMime("text/*")
@Path("/library")
public class Library {

   @GET
   @ProduceMime("application/json")
   public String getJSON() {...}


   @GET
   public String get() {...}

So, if the client sends:

GET /library
Accept: application/json

The getJSON() method would be invoked

Preferences

@ConsumeMime? and @ProduceMime? can list multiple media types that they support. The client's Accept header can also send multiple types it might like to receive. More specific media types are chosen first. The client Accept header or @ProduceMime? @ConsumeMime? can also specify weighted preferences that are used to match up requests with resource methods. This is best explained by RFC 2616 section 14.1 . Resteasy supports this complex way of doing content negotiation.

Variants

A variant in JAX-RS is a combination of media type, content-language, and content encoding as well as etags, last modified headers, and other preconditions. This is a more complex form of content negotiation that is done programmatically by the application developer using the javax.ws.rs.Variant, VarianListBuilder?, and Request objects. Request is injected via @HttpContext. Read the javadoc for more info on these.


Other languages:

Log in to make links between pages

The page last changed on Mon Feb 18 14:39:44 EST 2008 by bill.burke@jboss.com