<div dir="ltr" style="text-align: justify;" trbidi="on"><b>JAX-RS</b> annotations for resource classes let you extract specific parts or values from a Uniform Resource Identifier (URI) or request header.</p>
<table style="-webkit-text-stroke-width: 0px; border-collapse: collapse; border: 1px solid black; color: black; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; margin-bottom: 15px; margin-top: 15px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<tbody>
<tr>
<th align="left" scope="column" style="border: thin solid; padding: 4px 4px 6px;" valign="top">
<div style="margin: 0px; padding: 0px;">Annotation</div>
</th>
<th align="left" scope="column" style="border: thin solid; padding: 4px 4px 6px;" valign="top">
<div style="margin: 0px; padding: 0px;">Description</div>
</th>
</tr>
<tr>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">@Context</div>
</td>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">Injects information into a class field, bean property, or method parameter</div>
</td>
</tr>
<tr>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">@CookieParam</div>
</td>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">Extracts information from cookies declared in the cookie request header</div>
</td>
</tr>
<tr>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">@FormParam</div>
</td>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">Extracts information from a request representation whose content type is ;application/x-www-form-urlencoded</div>
</td>
</tr>
<tr>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">@HeaderParam</div>
</td>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">Extracts the value of a header</div>
</td>
</tr>
<tr>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">@MatrixParam</div>
</td>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">Extracts the value of a URI matrix parameter</div>
</td>
</tr>
<tr>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">@PathParam</div>
</td>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">Extracts the value of a URI template parameter</div>
</td>
</tr>
<tr>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">@QueryParam</div>
</td>
<td align="left" scope="row" style="border: thin solid; color: black; padding: 4px 4px 6px;" valign="top">
<div style="color: black; margin: 0px; padding: 0px;">Extracts the value of a URI query parameter</div>
</td>
</tr>
</tbody>
</table>
<div align='center' id="ads-id"></div>
<p><b><br />
Extracting Path Parameters</b><br />
URI path templates are URIs with variables embedded within the URI syntax. The<i><b> @PathParam</b></i> annotation lets you use variable URI path fragments when you call a method.</p>
<p> The following code snippet shows how to extract the last name of an employee when the employee’s email address is provided:</p>
<pre class="highlight" name="code">@Path(/employees/"{firstname}.{lastname}@{domain}.com")
public class EmpResource {

 @GET
 @Produces("text/xml")
 public String getEmployeelastname(@PathParam("lastname") String lastName) {
 ...
 }
}
</pre>
<p>In this example, the <i><b>@Path</b></i> annotation defines the URI variables (or path parameters) {<i><b>firstname</b></i>} , {<i><b>lastname</b></i>}, and {<i><b>domain</b></i>}. The <i><b>@PathParam</b></i> in the method parameter of the request method extracts the last name from the email address.</p>
<p> If your HTTP request is GET<i><b> /employees/dinesh.rajput@example.com,</b></i> the value &#8220;<b>rajput</b>&#8221; is injected into {<b>lastname</b>}.</p>
<p> You can specify several path parameters in one URI.</p>
<p> You can declare a regular expression with a URI variable. For example, if it is required that the last name must consist only of lower and upper case characters, you can declare the following regular expression:</p>
<pre class="highlight" name="code">@Path(/employees/{"firstname}.{lastname[a-zA-Z]*}@{domain}.com")
</pre>
<p>If the last name does not match the regular expression, a 404 response is returned.<br />
<b><br />
Extracting Query Parameters</b><br />
Use the <i><b>@QueryParam</b></i> annotation to extract query parameters from the query component of the request URI.</p>
<p> For instance, to query all employees who have joined within a specific range of years, use a method signature like the following:</p>
<pre class="highlight" name="code">@Path(/employees/")
@GET
public Response getEmployees(
 @DefaultValue("2002") @QueryParam("minyear") int minyear,
 @DefaultValue("2010") @QueryParam("maxyear") int maxyear)
 {...}
</pre>
<p>This code snippet defines two query parameters, minyear and maxyear. The following HTTP request would query for all employees who have joined between 1999 and 2009:</p>
<p> GET<i><b> /employees?maxyear=2009&;minyear=1999</b></i><br />
The<i><b> @DefaultValue</b></i> annotation defines a default value, which is to be used if no values are provided for the query parameters. By default, JAX-RS assigns a null value for Object values and zero for primitive data types. You can use the <i><b>@DefaultValue</b></i> annotation to eliminate null or zero values and define your own default values for a parameter.</p>
<p><b>Extracting Form Data</b><br />
Use the @FormParam annotation to extract form parameters from HTML forms. For example, the following form accepts the name, address, and manager’s name of an employee:</p>
<pre class="highlight" name="code"><;form action="http://example.com/employees/" method="post">;

<;fieldset>;
Employee name: <;input name="empname" tabindex="1" type="text" />; 
Employee address: <;input name="empaddress" tabindex="2" type="text" />; 
Manager name: <;input name="managername" tabindex="3" type="text" />; <;/fieldset>;
<;/form>;
</pre>
<p>Use the following code snippet to extract the manager name from this HTML form:</p>
<pre class="highlight" name="code">@POST
@Consumes("application/x-www-form-urlencoded")
public void post(@FormParam("managername") String managername) {
 // Store the value
 ...
}
</pre>
<p>To obtain a map of form parameter names to values, use a code snippet like the following:</p>
<pre class="highlight" name="code">@POST
@Consumes("application/x-www-form-urlencoded")
public void post(MultivaluedMap<;String. String>; formParams) {
 // Store the message
}
</pre>
<p>
<b>Extracting the Java Type of a Request or Response</b><br />
The javax.ws.rs.core.Context annotation retrieves the Java types related to a request or response.</p>
<p> The javax.ws.rs.core.UriInfo interface provides information about the components of a request URI. The following code snippet shows how to obtain a map of query and path parameter names to values:</p>
<pre class="highlight" name="code">@GET
public String getParams(@Context UriInfo ui) {
 MultivaluedMap<;String, String>; queryParams = ui.getQueryParameters();
 MultivaluedMap<;String, String>; pathParams = ui.getPathParameters();
}
</pre>
<p>The javax.ws.rs.core.HttpHeaders interface provides information about request headers and cookies. The following code snippet shows how to obtain a map of header and cookie parameter names to values:</p>
<pre class="highlight" name="code">@GET
public String getHeaders(@Context HttpHeaders hh) {
 MultivaluedMap<;String, String>; headerParams = hh.getRequestHeaders();
 MultivaluedMap<;String, Cookie>; pathParams = hh.getCookies();
}
</pre>
<p>
<i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank">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/2013/06/using-consumes-and-produces-to.html">Previous </a><;<; ; ; || <a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/06/using-jax-rs-with-jaxb.html">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/using-consumes-and-produces-to/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/using-jax-rs-with-jaxb/">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: '426'});
});
</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…