Content Negotiation Consumes Produces

Start course
1h 8m

This course takes an in-depth look at Java API for RESTful Web Services, otherwise known as JAX-RS. We also run through some example exam questions that will be useful for anyone who wants to take Oracle's Java EE exam.

Learning Objectives

  • Understand the fundamentals of JAX-RS
  • Learn about the main concepts and components of JAX-RS

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.


Basic knowledge of Java programming.


Hello, dear friends. In this video, we will examine content negotiation, consumes, and produces annotations. Let's begin. Content negotiation is a mechanism defined in the HTTP specification, that makes possible to serve different versions of a document or more generally of a resource at the same URL, so that user agents can choose which version fit their capacities the best. One of the most classical usage of this mechanism is to serve an image as both GIF and PNG, so that browsers that don't understand PNG still get the GIF version. To summarize how this works, it's enough to say that user agents are supposed to send an HTTP header with the various MIME type they understand and with indications of how well they understand it. Then, the server replies with the version of the resource that fits the user agent's needs.

Most HTTP responses include an entity which contains information for interpretation by a human user. Naturally, it's desirable to supply the user with the best available entity corresponding to the request. Unfortunately for servers and caches, not all users have the same preferences for what is best and not all user agents are equally capable of rendering all entity types. For that reason, HTTP has provisions for several mechanisms for content negotiation, the process of selecting the best representation for a given response when there are multiple representations available. Any response containing an entity body may be subject to negotiation, including error responses. There are two kinds of content negotiation which are possible in HTTP, server-driven and agent-driven negotiation. These two kinds of negotiations are orthogonal and thus may be used separately or in combination.

One method of combination referred to as transparent negotiation occurs when a cache uses the agent-driven negotiation information provided by the origin server in order to provide server-driven negotiation for subsequent requests. For us, that's enough. Let's look into MIME types. Multipurpose Internet Mail Extension type as MIME is also known as content type. It is an HTTP header that provides the description about what you are sending to the browser. MIME is an Internet standard that is used for extending the limited capabilities of email by allowing the insertion of sounds, images, and text in a message. The features provided by MIME to the email services are: It supports the non-ASCII characters, it supports the multiple attachments in a single message, it supports the attachment which contains executable audio, images and video files etc., it supports the unlimited message length.

There are many content types. The most commonly used content types include text/html, text/plain, application/msword, application/ jar, etc. We will use these content types in consumes and produces annotations. So, that's the time to learn consume and produces annotations. First, I want to start with consumes. Consumes annotation works together with POST and PUT. It tells the framework to which method to delegate the incoming request. Specifically, the client sets the content type http header and the framework delegates the request to the corresponding handling method. This image shows us the usage of consumes annotation. In this example, we're telling the framework that the update user method accepts an input stream of MIME type application, XML which is stored in the variable representation.

Therefore, a client connecting to the web service through the URI, users must send an http PUT method request containing the http header content type with a value of application XML. Or let's look at this image. The doPost method defaults to the MIME media type of the consumes annotation at the class level. The doPost two method overrides the class level consumes annotation to specify that it can accept URL application, JSON. I hope you now have a better understanding of consumes annotations. Let's move on to produces annotations. The produces annotation is used to specify the MIME media types or representations a resource can produce and send back to the client. If produces is applied at the class level, all the methods in a resource can produce the specified MIME types by default. If applied at the method level, the annotation overrides any produces annotation applied at the class level. If no methods in a resource are able to produce the MIME type in a client request, the JAX-RS runtime sends back an http 406 none acceptable error. Specifically, the client sends an http request together with an accept http header that maps directly to the content type the method produces.

So, if the value of the http header accept is application XML, the method handling the request returns a stream of MIME type application, XML. This annotation can also be used with multiple methods in the same resource class. Let's look at this image. As you can see, the usage of produces annotation is like consumes annotation. Or let's look at another example. The doGet method defaults to the MIME media type of the produces annotation at the class level. The doGet two methods produces annotation overrides the class level produces setting, and specifies that the method can produce html rather than plain text. More than one media type may be declared in the same produces declaration. Let's examine this code snip. The doGet method will get invoked if either of the media types application, XML and application JSON is acceptable. If both are equally acceptable, the former will be chosen because it occurs first. That's it for now. In the next video, we will look into extracting request parameters. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics