@Consumes and @Produces annotations
One can use @Consumes and @Produces annotations on class level as well as method level.
Using the @Consumes and @Produces annotations on a method overrides any annotations on the resource class for a method argument or return type.
In the absence of either of these annotations, support for any media type (*/*) is Assumed.
@Path("/item")
@Produces(MediaType.TEXT_PLAIN)
public class ItemRestService {
@GET
public Response getAsPlainText() {
// ...
}
@GET
@Produces(MediaType.TEXT_HTML)
public Response getAsHtml() {
// ...
}
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getAsJsonAndXML() {
// ...
}
@PUT
@Consumes(MediaType.TEXT_PLAIN)
public void putName(String itemName) {
// ...
}
}
If a RESTful web service is capable of producing more than one media type, the targeted method will correspond to the most acceptable media type, as declared by the client in the Accept header of the HTTP request. For example, if the Accept header is:
Accept: text/plain
and the URI is /item, the getAsPlainText() method will be invoked. But the client could have used the following HTTP header:
Accept: text/plain; q=0.8, text/html
This header declares that the client can accept media types of text/plain and text/html but prefers the latter using the quality factor (or preference weight) of 0.8 (“I prefer text/html, but send me text/plain if it is the best available after an 80% markdown in quality”).
By including such header and pointing at the /item URI, the getAsHtml() method will be invoked.