Using 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 that is used to exchange data.
About this task
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.
Depending on the requirements of your application, 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 because JSON is easy to consume. However, other clients prefer XML.
Use content negotiation if you want 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. You can use content negotiation that is based on the URL, based on a request parameter, or based on HTTP headers.
Procedure
Results
You have implemented content negotiation to determine the formats for resources to represent data.