<div dir="ltr" style="text-align: justify;" trbidi="on">In this tutorial, we will discuss about the two ways to get <b>HTTP request header in JAX-RS</b> :</p>
<ol>
<li>Inject directly with <i><b>@HeaderParam</b></i></li>
<li>Pragmatically via <i><b>@Context</b></i></li>
</ol>
<p><b>1. <i>@HeaderParam</i> Example</b></p>
<p> In this example, it gets the browser “<b>user-agent</b>” from request header.</p>
<pre class="highlight" name="code">import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
 
@Path("/employee")
public class EmployeeController {
 
 @GET
 @Path("/get")
 public Response addEmployee(@HeaderParam("user-agent") String userAgent) {
 
 return Response.status(200)
 .entity("addEmployee is called, userAgent : " + userAgent)
 .build();
 
 }
 
}
</pre>
<div align='center' id="ads-id"></div>
<p>Access via URI pattern <i><b>&#8220;http://localhost:8181/sdnext/doj/employee/get&#8221;</b></i>, with <b>Chrome</b>, see following result :<br />
<b><br />
</b> <b>addEmployee is called, userAgent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36</b><br />
<b><br />
</b> <b>2. <i>@Context</i> Example</b></p>
<p> Alternatively, you can use <i><b>@Context </b></i>to get <i><b>&#8220;javax.ws.rs.core.HttpHeaders&#8221;</b></i> directly, see equivalent version to get browser &#8220;<i><b>user-agent</b></i>&#8220;.</p>
<pre class="highlight" name="code">import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
 
@Path("/employee")
public class EmployeeController {
 
 @GET
 @Path("/get")
 public Response addEmployee(@Context HttpHeaders headers) {
 
 String userAgent = headers.getRequestHeader("user-agent").get(0);
 
 return Response.status(200)
 .entity("addEmployee is called, userAgent : " + userAgent)
 .build();
 
 }
 
}
</pre>
<p>Access via URI pattern <i><b>&#8220;http://localhost:8181/sdnext/doj/employee/get&#8221;</b></i>, with <b>Chrome</b>, see following result :<br />
<b><br />
</b> <b>addEmployee is called, userAgent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36</b><br />
<b><br />
</b></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">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/jax-rs-formparam-example.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/download-text-file-from-jax-rs.html">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/jax-rs-formparam-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/download-text-file-from-jax-rs/">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: '419'});
});
</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…