Annotations for Field and Bean Properties of Resource Classes

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on"><b>JAX-RS<&sol;b> annotations for resource classes let you extract specific parts or values from a Uniform Resource Identifier &lpar;URI&rpar; or request header&period;<&sol;p>&NewLine;<table style&equals;"-webkit-text-stroke-width&colon; 0px&semi; border-collapse&colon; collapse&semi; border&colon; 1px solid black&semi; color&colon; black&semi; font-family&colon; Arial&comma; Helvetica&comma; FreeSans&comma; Luxi-sans&comma; 'Nimbus Sans L'&comma; sans-serif&semi; font-size&colon; 12px&semi; font-style&colon; normal&semi; font-variant&colon; normal&semi; font-weight&colon; normal&semi; letter-spacing&colon; normal&semi; line-height&colon; normal&semi; margin-bottom&colon; 15px&semi; margin-top&colon; 15px&semi; orphans&colon; auto&semi; text-align&colon; start&semi; text-indent&colon; 0px&semi; text-transform&colon; none&semi; white-space&colon; normal&semi; widows&colon; auto&semi; word-spacing&colon; 0px&semi;">&NewLine;<tbody>&NewLine;<tr>&NewLine;<th align&equals;"left" scope&equals;"column" style&equals;"border&colon; thin solid&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Annotation<&sol;div>&NewLine;<&sol;th>&NewLine;<th align&equals;"left" scope&equals;"column" style&equals;"border&colon; thin solid&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"margin&colon; 0px&semi; padding&colon; 0px&semi;">Description<&sol;div>&NewLine;<&sol;th>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">&commat;Context<&sol;div>&NewLine;<&sol;td>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">Injects information into a class field&comma; bean property&comma; or method parameter<&sol;div>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">&commat;CookieParam<&sol;div>&NewLine;<&sol;td>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">Extracts information from cookies declared in the cookie request header<&sol;div>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">&commat;FormParam<&sol;div>&NewLine;<&sol;td>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">Extracts information from a request representation whose content type is&nbsp&semi;application&sol;x-www-form-urlencoded<&sol;div>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">&commat;HeaderParam<&sol;div>&NewLine;<&sol;td>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">Extracts the value of a header<&sol;div>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">&commat;MatrixParam<&sol;div>&NewLine;<&sol;td>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">Extracts the value of a URI matrix parameter<&sol;div>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">&commat;PathParam<&sol;div>&NewLine;<&sol;td>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">Extracts the value of a URI template parameter<&sol;div>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">&commat;QueryParam<&sol;div>&NewLine;<&sol;td>&NewLine;<td align&equals;"left" scope&equals;"row" style&equals;"border&colon; thin solid&semi; color&colon; black&semi; padding&colon; 4px 4px 6px&semi;" valign&equals;"top">&NewLine;<div style&equals;"color&colon; black&semi; margin&colon; 0px&semi; padding&colon; 0px&semi;">Extracts the value of a URI query parameter<&sol;div>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p><b><br &sol;>&NewLine;Extracting Path Parameters<&sol;b><br &sol;>&NewLine;URI path templates are URIs with variables embedded within the URI syntax&period; The<i><b> &commat;PathParam<&sol;b><&sol;i> annotation lets you use variable URI path fragments when you call a method&period;<&sol;p>&NewLine;<p> The following code snippet shows how to extract the last name of an employee when the employee’s email address is provided&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;Path&lpar;&sol;employees&sol;"&lbrace;firstname&rcub;&period;&lbrace;lastname&rcub;&commat;&lbrace;domain&rcub;&period;com"&rpar;&NewLine;public class EmpResource &lbrace;&NewLine;&NewLine; &commat;GET&NewLine; &commat;Produces&lpar;"text&sol;xml"&rpar;&NewLine; public String getEmployeelastname&lpar;&commat;PathParam&lpar;"lastname"&rpar; String lastName&rpar; &lbrace;&NewLine; &period;&period;&period;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>In this example&comma; the <i><b>&commat;Path<&sol;b><&sol;i> annotation defines the URI variables &lpar;or path parameters&rpar; &lbrace;<i><b>firstname<&sol;b><&sol;i>&rcub; &comma; &lbrace;<i><b>lastname<&sol;b><&sol;i>&rcub;&comma; and &lbrace;<i><b>domain<&sol;b><&sol;i>&rcub;&period; The <i><b>&commat;PathParam<&sol;b><&sol;i> in the method parameter of the request method extracts the last name from the email address&period;<&sol;p>&NewLine;<p> If your HTTP request is GET<i><b> &sol;employees&sol;dinesh&period;rajput&commat;example&period;com&comma;<&sol;b><&sol;i> the value &&num;8220&semi;<b>rajput<&sol;b>&&num;8221&semi; is injected into &lbrace;<b>lastname<&sol;b>&rcub;&period;<&sol;p>&NewLine;<p> You can specify several path parameters in one URI&period;<&sol;p>&NewLine;<p> You can declare a regular expression with a URI variable&period; For example&comma; if it is required that the last name must consist only of lower and upper case characters&comma; you can declare the following regular expression&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;Path&lpar;&sol;employees&sol;&lbrace;"firstname&rcub;&period;&lbrace;lastname&lbrack;a-zA-Z&rsqb;&ast;&rcub;&commat;&lbrace;domain&rcub;&period;com"&rpar;&NewLine;<&sol;pre>&NewLine;<p>If the last name does not match the regular expression&comma; a 404 response is returned&period;<br &sol;>&NewLine;<b><br &sol;>&NewLine;Extracting Query Parameters<&sol;b><br &sol;>&NewLine;Use the <i><b>&commat;QueryParam<&sol;b><&sol;i> annotation to extract query parameters from the query component of the request URI&period;<&sol;p>&NewLine;<p> For instance&comma; to query all employees who have joined within a specific range of years&comma; use a method signature like the following&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;Path&lpar;&sol;employees&sol;"&rpar;&NewLine;&commat;GET&NewLine;public Response getEmployees&lpar;&NewLine; &commat;DefaultValue&lpar;"2002"&rpar; &commat;QueryParam&lpar;"minyear"&rpar; int minyear&comma;&NewLine; &commat;DefaultValue&lpar;"2010"&rpar; &commat;QueryParam&lpar;"maxyear"&rpar; int maxyear&rpar;&NewLine; &lbrace;&period;&period;&period;&rcub;&NewLine;<&sol;pre>&NewLine;<p>This code snippet defines two query parameters&comma; minyear and maxyear&period; The following HTTP request would query for all employees who have joined between 1999 and 2009&colon;<&sol;p>&NewLine;<p> GET<i><b> &sol;employees&quest;maxyear&equals;2009&amp&semi;minyear&equals;1999<&sol;b><&sol;i><br &sol;>&NewLine;The<i><b> &commat;DefaultValue<&sol;b><&sol;i> annotation defines a default value&comma; which is to be used if no values are provided for the query parameters&period; By default&comma; JAX-RS assigns a null value for Object values and zero for primitive data types&period; You can use the <i><b>&commat;DefaultValue<&sol;b><&sol;i> annotation to eliminate null or zero values and define your own default values for a parameter&period;<&sol;p>&NewLine;<p><b>Extracting Form Data<&sol;b><br &sol;>&NewLine;Use the &commat;FormParam annotation to extract form parameters from HTML forms&period; For example&comma; the following form accepts the name&comma; address&comma; and manager’s name of an employee&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;form action&equals;"http&colon;&sol;&sol;example&period;com&sol;employees&sol;" method&equals;"post"&gt&semi;&NewLine;&NewLine;&lt&semi;fieldset&gt&semi;&NewLine;Employee name&colon; &lt&semi;input name&equals;"empname" tabindex&equals;"1" type&equals;"text" &sol;&gt&semi; &NewLine;Employee address&colon; &lt&semi;input name&equals;"empaddress" tabindex&equals;"2" type&equals;"text" &sol;&gt&semi; &NewLine;Manager name&colon; &lt&semi;input name&equals;"managername" tabindex&equals;"3" type&equals;"text" &sol;&gt&semi; &lt&semi;&sol;fieldset&gt&semi;&NewLine;&lt&semi;&sol;form&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>Use the following code snippet to extract the manager name from this HTML form&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;POST&NewLine;&commat;Consumes&lpar;"application&sol;x-www-form-urlencoded"&rpar;&NewLine;public void post&lpar;&commat;FormParam&lpar;"managername"&rpar; String managername&rpar; &lbrace;&NewLine; &sol;&sol; Store the value&NewLine; &period;&period;&period;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>To obtain a map of form parameter names to values&comma; use a code snippet like the following&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;POST&NewLine;&commat;Consumes&lpar;"application&sol;x-www-form-urlencoded"&rpar;&NewLine;public void post&lpar;MultivaluedMap&lt&semi;String&period; String&gt&semi; formParams&rpar; &lbrace;&NewLine; &sol;&sol; Store the message&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Extracting the Java Type of a Request or Response<&sol;b><br &sol;>&NewLine;The javax&period;ws&period;rs&period;core&period;Context annotation retrieves the Java types related to a request or response&period;<&sol;p>&NewLine;<p> The javax&period;ws&period;rs&period;core&period;UriInfo interface provides information about the components of a request URI&period; The following code snippet shows how to obtain a map of query and path parameter names to values&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;GET&NewLine;public String getParams&lpar;&commat;Context UriInfo ui&rpar; &lbrace;&NewLine; MultivaluedMap&lt&semi;String&comma; String&gt&semi; queryParams &equals; ui&period;getQueryParameters&lpar;&rpar;&semi;&NewLine; MultivaluedMap&lt&semi;String&comma; String&gt&semi; pathParams &equals; ui&period;getPathParameters&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>The javax&period;ws&period;rs&period;core&period;HttpHeaders interface provides information about request headers and cookies&period; The following code snippet shows how to obtain a map of header and cookie parameter names to values&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;GET&NewLine;public String getHeaders&lpar;&commat;Context HttpHeaders hh&rpar; &lbrace;&NewLine; MultivaluedMap&lt&semi;String&comma; String&gt&semi; headerParams &equals; hh&period;getRequestHeaders&lpar;&rpar;&semi;&NewLine; MultivaluedMap&lt&semi;String&comma; Cookie&gt&semi; pathParams &equals; hh&period;getCookies&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<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">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">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;2013&sol;06&sol;using-consumes-and-produces-to&period;html">Previous <&sol;a>&lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;06&sol;jax-rs-web-service-tutorial&period;html">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;06&sol;using-jax-rs-with-jaxb&period;html">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;using-consumes-and-produces-to&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;using-jax-rs-with-jaxb&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; '426'&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