+

Search Tips   |   Advanced Search

Use content negotiation to serve multiple content types in JAX-RS applications

One of the advantages of RESTful applications is the ability to return different representations of resources. With Representational State Transfer (REST), clients and servers can exchange resources of the same media type or use differing media types. Content negotiation enables clients and servers to agree on the content format used to exchange data.

Resources are represented by many different formats. XML, JavaScript Object Notation (JSON), Atom, plain text, PNG, JPEG, GIF, and custom or proprietary formats are used to represent resources. Representational State Transfer (REST) provides the flexibility to represent a single resource in multiple formats.

Resources can return representations in a single format or in different formats, depending on the request. For example, resources accessed using JavaScript clients might prefer JSON representations. However, other clients prefer XML.

Use content negotiation to serve multiple formats to clients. Content negotiation is the method in which the client and server agree on the response content type to use. There are three types of content negotiation that affect the response. We can use content negotiation based on the URL, based on a request parameter, or based on HTTP headers.

  1. Configure the development environment.

  2. Define resources in JAX-RS web applications.

  3. Configure the JAX-RS application.

  4. Implement content negotiation to serve multiple content types.

  5. Assemble JAX-RS web applications.

  6. Deploy JAX-RS web applications.


Results

You have implemented content negotiation to determine the formats for resources to represent data.


Subtopics


Related tasks

  • Implement content negotiation based on URL patterns
  • Implement content negotiation based on HTTP headers
  • Implement content negotiation based on request parameters

  • Web services specifications and APIs