<div dir="ltr" style="text-align: justify;">The information sent to a resource and then passed back to the client is specified as a MIME media type in the headers of an HTTP request or response. You can specify which MIME media types of representations a resource can respond to or produce by using the following annotations:</p>
<ul>
<li>javax.ws.rs.Consumes</li>
<li>javax.ws.rs.Produces</li>
</ul>
<p>By default, a resource class can respond to and produce all MIME media types of representations specified in the HTTP request and response headers.</p>
<h2><b>The @Produces Annotation</b></h2>
<p>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 annotations applied at the class level.<br />
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 Not Acceptable” error.<br />
The value of @Produces is an array of String of MIME types. For example:</p>
<pre class="highlight">@Produces({"image/jpeg,image/png"}) 
</pre>
<p>The following example shows how to apply @Produces at both the class and method levels:</p>
<pre class="highlight">@Path("/myResource") 
@Produces("text/plain") 
public class SomeResource { 
 @GET 
 public String doGetAsPlainText() { 
 ... 
 } 
 
 @GET 
 @Produces("text/html") 
 public String doGetAsHtml() { 
 ... 
 } 
} 
</pre>
<div id="ads-id" align="center"></div>
<p>The doGetAsPlainText method defaults to the MIME media type of the @Produces annotation at the class level. The <i><b>doGetAsHtml </b></i>method’s<i><b> @Produces</b></i> annotation overrides the class-level <i><b>@Produces</b></i> setting and specifies that the method can produce HTML rather than plain text.</p>
<p>If a resource class is capable of producing more than one MIME media type, the resource method chosen will correspond to the most acceptable media type as declared by the client. More specifically, the Accept header of the HTTP request declares what is most acceptable. For example, if the Accept header is Accept: <i><b>text/plain,</b></i> the <i><b>doGetAsPlainText </b></i>method will be invoked. Alternatively, if the Accept header is Accept: <i><b>text/plain;q=0.9, text/html</b></i>, which declares that the client can accept media types of <i><b>text/plain</b></i> and <i><b>text/html </b></i>but prefers the latter, the <i><b>doGetAsHtml </b></i>method will be invoked.</p>
<p>More than one media type may be declared in the same @Produces declaration. The following code example shows how this is done:</p>
<pre class="highlight">@Produces({"application/xml", "application/json"}) 
public String doGetAsXmlOrJson() { 
 ... 
} 
</pre>
<p>The <i><b>doGetAsXmlOrJson </b></i>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. The preceding examples refer explicitly to MIME media types for clarity. It is possible to refer to constant values, which may reduce typographical errors.<br />
<b> </b></p>
<h2><b>The @Consumes Annotation</b></h2>
<p>The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client. If @Consumes is applied at the class level, all the response methods accept the specified MIME types by default. If applied at the method level, @Consumes overrides any @Consumes annotations applied at the class level.<br />
If a resource is unable to consume the MIME type of a client request, the <i><b>JAX-RS</b></i> runtime sends back an HTTP 415 (“Unsupported Media Type”) error.<br />
The value of @Consumes is an array of String of acceptable MIME types. For example:</p>
<pre class="highlight">@Consumes({"text/plain,text/html"}) 
</pre>
<p>The following example shows how to apply <i><b>@Consumes</b></i> at both the class and method levels:</p>
<pre class="highlight">@Path("/myResource") 
@Consumes("multipart/related") 
public class SomeResource { 
 @POST 
 public String doPost(MimeMultipart mimeMultipartData) { 
 ... 
 } 
 
 @POST 
 @Consumes("application/x-www-form-urlencoded") 
 public String doPost2(FormURLEncodedProperties formData) { 
 ... 
 } 
} 
</pre>
<p>The <i><b>doPost </b></i>method defaults to the MIME media type of the <i><b>@Consumes</b></i> annotation at the class level. The <i><b>doPost2 </b></i>method overrides the class level <i><b>@Consumes</b></i> annotation to specify that it can accept URL-encoded form data.</p>
<p><b>Query parameters</b> are extracted from the request URI query parameters and are specified by using the<i><b> javax.ws.rs.QueryParam</b></i> annotation in the method parameter arguments. The following example, from the sparklines sample application, demonstrates using <i><b>@QueryParam</b></i> to extract query parameters from the Query component of the request URL:</p>
<pre class="highlight">@Path("smooth") 
@GET 
public Response smooth( 
 @DefaultValue("2") @QueryParam("step") int step, 
 @DefaultValue("true") @QueryParam("min-m") boolean hasMin, 
 @DefaultValue("true") @QueryParam("max-m") boolean hasMax, 
 @DefaultValue("true") @QueryParam("last-m") boolean hasLast, 
 @DefaultValue("blue") @QueryParam("min-color") ColorParam minColor, 
 @DefaultValue("green") @QueryParam("max-color") ColorParam maxColor, 
 @DefaultValue("red") @QueryParam("last-color") ColorParam lastColor 
 ) { ... } 
</pre>
<p>If the query parameter step exists in the query component of the request URI, the value of step will be extracted and parsed as a 32-bit signed integer and assigned to the step method parameter. If step does not exist, a default value of 2, as declared in the <i><b>@DefaultValue</b></i> annotation, will be assigned to the step method parameter. If the step value cannot be parsed as a 32-bit signed integer, an HTTP 400 (“Client Error”) response is returned.</p>
<p><i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank" rel="noopener">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank" rel="noopener">Wikipedia for REST Web Service</a></b></i></p>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/overview-of-jax-rs-application/">Previous </a><;<; || <a href="https://dineshonjava.com/jax-rs-web-service-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/annotations-for-field-and-bean/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/overview-of-jax-rs-application/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/annotations-for-field-and-bean/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '427'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…