REST

@Consumes and @Produces Annotation to Customize Requests and Responses

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">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&period; You can specify which MIME media types of representations a resource can respond to or produce by using the following annotations&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>javax&period;ws&period;rs&period;Consumes<&sol;li>&NewLine;<li>javax&period;ws&period;rs&period;Produces<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>By default&comma; a resource class can respond to and produce all MIME media types of representations specified in the HTTP request and response headers&period;<&sol;p>&NewLine;<h2><b>The &commat;Produces Annotation<&sol;b><&sol;h2>&NewLine;<p>The &commat;Produces annotation is used to specify the MIME media types or representations a resource can produce and send back to the client&period; If &commat;Produces is applied at the class level&comma; all the methods in a resource can produce the specified MIME types by default&period; If applied at the method level&comma; the annotation overrides any &commat;Produces annotations applied at the class level&period;<br &sol;>&NewLine;If no methods in a resource are able to produce the MIME type in a client request&comma; the JAX-RS runtime sends back an HTTP &OpenCurlyDoubleQuote;406 Not Acceptable” error&period;<br &sol;>&NewLine;The value of &commat;Produces is an array of String of MIME types&period; For example&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Produces&lpar;&lbrace;"image&sol;jpeg&comma;image&sol;png"&rcub;&rpar; &NewLine;<&sol;pre>&NewLine;<p>The following example shows how to apply &commat;Produces at both the class and method levels&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Path&lpar;"&sol;myResource"&rpar; &NewLine;&commat;Produces&lpar;"text&sol;plain"&rpar; &NewLine;public class SomeResource &lbrace; &NewLine; &commat;GET &NewLine; public String doGetAsPlainText&lpar;&rpar; &lbrace; &NewLine; &period;&period;&period; &NewLine; &rcub; &NewLine; &NewLine; &commat;GET &NewLine; &commat;Produces&lpar;"text&sol;html"&rpar; &NewLine; public String doGetAsHtml&lpar;&rpar; &lbrace; &NewLine; &period;&period;&period; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p>The doGetAsPlainText method defaults to the MIME media type of the &commat;Produces annotation at the class level&period; The <i><b>doGetAsHtml <&sol;b><&sol;i>method’s<i><b> &commat;Produces<&sol;b><&sol;i> annotation overrides the class-level <i><b>&commat;Produces<&sol;b><&sol;i> setting and specifies that the method can produce HTML rather than plain text&period;<&sol;p>&NewLine;<p>If a resource class is capable of producing more than one MIME media type&comma; the resource method chosen will correspond to the most acceptable media type as declared by the client&period; More specifically&comma; the Accept header of the HTTP request declares what is most acceptable&period; For example&comma; if the Accept header is Accept&colon; <i><b>text&sol;plain&comma;<&sol;b><&sol;i> the <i><b>doGetAsPlainText <&sol;b><&sol;i>method will be invoked&period; Alternatively&comma; if the Accept header is Accept&colon; <i><b>text&sol;plain&semi;q&equals;0&period;9&comma; text&sol;html<&sol;b><&sol;i>&comma; which declares that the client can accept media types of <i><b>text&sol;plain<&sol;b><&sol;i> and <i><b>text&sol;html <&sol;b><&sol;i>but prefers the latter&comma; the <i><b>doGetAsHtml <&sol;b><&sol;i>method will be invoked&period;<&sol;p>&NewLine;<p>More than one media type may be declared in the same &commat;Produces declaration&period; The following code example shows how this is done&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Produces&lpar;&lbrace;"application&sol;xml"&comma; "application&sol;json"&rcub;&rpar; &NewLine;public String doGetAsXmlOrJson&lpar;&rpar; &lbrace; &NewLine; &period;&period;&period; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>The <i><b>doGetAsXmlOrJson <&sol;b><&sol;i>method will get invoked if either of the media types application&sol;xml and application&sol;json is acceptable&period; If both are equally acceptable&comma; the former will be chosen because it occurs first&period; The preceding examples refer explicitly to MIME media types for clarity&period; It is possible to refer to constant values&comma; which may reduce typographical errors&period;<br &sol;>&NewLine;<b> <&sol;b><&sol;p>&NewLine;<h2><b>The &commat;Consumes Annotation<&sol;b><&sol;h2>&NewLine;<p>The &commat;Consumes annotation is used to specify which MIME media types of representations a resource can accept&comma; or consume&comma; from the client&period; If &commat;Consumes is applied at the class level&comma; all the response methods accept the specified MIME types by default&period; If applied at the method level&comma; &commat;Consumes overrides any &commat;Consumes annotations applied at the class level&period;<br &sol;>&NewLine;If a resource is unable to consume the MIME type of a client request&comma; the <i><b>JAX-RS<&sol;b><&sol;i> runtime sends back an HTTP 415 &lpar;&OpenCurlyDoubleQuote;Unsupported Media Type”&rpar; error&period;<br &sol;>&NewLine;The value of &commat;Consumes is an array of String of acceptable MIME types&period; For example&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Consumes&lpar;&lbrace;"text&sol;plain&comma;text&sol;html"&rcub;&rpar; &NewLine;<&sol;pre>&NewLine;<p>The following example shows how to apply <i><b>&commat;Consumes<&sol;b><&sol;i> at both the class and method levels&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Path&lpar;"&sol;myResource"&rpar; &NewLine;&commat;Consumes&lpar;"multipart&sol;related"&rpar; &NewLine;public class SomeResource &lbrace; &NewLine; &commat;POST &NewLine; public String doPost&lpar;MimeMultipart mimeMultipartData&rpar; &lbrace; &NewLine; &period;&period;&period; &NewLine; &rcub; &NewLine; &NewLine; &commat;POST &NewLine; &commat;Consumes&lpar;"application&sol;x-www-form-urlencoded"&rpar; &NewLine; public String doPost2&lpar;FormURLEncodedProperties formData&rpar; &lbrace; &NewLine; &period;&period;&period; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>The <i><b>doPost <&sol;b><&sol;i>method defaults to the MIME media type of the <i><b>&commat;Consumes<&sol;b><&sol;i> annotation at the class level&period; The <i><b>doPost2 <&sol;b><&sol;i>method overrides the class level <i><b>&commat;Consumes<&sol;b><&sol;i> annotation to specify that it can accept URL-encoded form data&period;<&sol;p>&NewLine;<p><b>Query parameters<&sol;b> are extracted from the request URI query parameters and are specified by using the<i><b> javax&period;ws&period;rs&period;QueryParam<&sol;b><&sol;i> annotation in the method parameter arguments&period; The following example&comma; from the sparklines sample application&comma; demonstrates using <i><b>&commat;QueryParam<&sol;b><&sol;i> to extract query parameters from the Query component of the request URL&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Path&lpar;"smooth"&rpar; &NewLine;&commat;GET &NewLine;public Response smooth&lpar; &NewLine; &commat;DefaultValue&lpar;"2"&rpar; &commat;QueryParam&lpar;"step"&rpar; int step&comma; &NewLine; &commat;DefaultValue&lpar;"true"&rpar; &commat;QueryParam&lpar;"min-m"&rpar; boolean hasMin&comma; &NewLine; &commat;DefaultValue&lpar;"true"&rpar; &commat;QueryParam&lpar;"max-m"&rpar; boolean hasMax&comma; &NewLine; &commat;DefaultValue&lpar;"true"&rpar; &commat;QueryParam&lpar;"last-m"&rpar; boolean hasLast&comma; &NewLine; &commat;DefaultValue&lpar;"blue"&rpar; &commat;QueryParam&lpar;"min-color"&rpar; ColorParam minColor&comma; &NewLine; &commat;DefaultValue&lpar;"green"&rpar; &commat;QueryParam&lpar;"max-color"&rpar; ColorParam maxColor&comma; &NewLine; &commat;DefaultValue&lpar;"red"&rpar; &commat;QueryParam&lpar;"last-color"&rpar; ColorParam lastColor &NewLine; &rpar; &lbrace; &period;&period;&period; &rcub; &NewLine;<&sol;pre>&NewLine;<p>If the query parameter step exists in the query component of the request URI&comma; the value of step will be extracted and parsed as a 32-bit signed integer and assigned to the step method parameter&period; If step does not exist&comma; a default value of 2&comma; as declared in the <i><b>&commat;DefaultValue<&sol;b><&sol;i> annotation&comma; will be assigned to the step method parameter&period; If the step value cannot be parsed as a 32-bit signed integer&comma; an HTTP 400 &lpar;&OpenCurlyDoubleQuote;Client Error”&rpar; response is returned&period;<&sol;p>&NewLine;<p><i><b>References<&sol;b><&sol;i><br &sol;>&NewLine;1&period;<b> <i><a href&equals;"http&colon;&sol;&sol;docs&period;oracle&period;com&sol;javaee&sol;6&sol;tutorial&sol;doc&sol;giepu&period;html" target&equals;"&lowbar;blank" rel&equals;"noopener">JAVA REST Web Services<&sol;a><&sol;i><&sol;b><br &sol;>&NewLine;2&period; <i><b><a href&equals;"http&colon;&sol;&sol;en&period;wikipedia&period;org&sol;wiki&sol;Web&lowbar;service" target&equals;"&lowbar;blank" rel&equals;"noopener">Wikipedia for REST Web Service<&sol;a><&sol;b><&sol;i><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;overview-of-jax-rs-application&sol;">Previous <&sol;a>&lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;annotations-for-field-and-bean&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;overview-of-jax-rs-application&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;annotations-for-field-and-bean&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '427'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago