You are previewing RESTful Java with JAX-RS.

RESTful Java with JAX-RS

Cover of RESTful Java with JAX-RS by Bill Burke Published by O'Reilly Media, Inc.
O'Reilly logo

JAX-RS and Conneg

The JAX-RS specification has a few facilities that help you manage conneg. It does method dispatching based on Accept header values. It allows you to view this content information directly. It also has complex negotiation APIs that allow you to deal with multiple decision points. Let’s look into each of these.

Method Dispatching

In previous chapters, we saw how the @Produces annotation denotes which media type a JAX-RS method should respond with. JAX-RS also uses this information to dispatch requests to the appropriate Java method. It matches the preferred media types listed in the Accept header of the incoming request to the metadata specified in @Produces annotations. Let’s look at a simple example:

public class CustomerResource {

   public Customer getCustomerXml(@PathParam("id") int id) {...}

   public String getCustomerText(@PathParam("id") int id) {...}

   public Customer getCustomerJson(@PathParam("id") int id) {...}

Here, we have three methods that all service the same URI but produce different data formats. JAX-RS can pick one of these methods based on what is in the Accept header. For example, let’s say a client made this request:

Accept: application/json;q=1.0, application/xml;q=0.5

The JAX-RS provider would dispatch this request to the getCustomerJson() method.

Leveraging ...

The best content for your career. Discover unlimited learning on demand for around $1/day.