JAX-RS Get HTTP Headers using @HeaderParam

In this tutorial we are going to see how to get HTTP request header using JAX-RS @HeaderParam annotation (Jersey RESTful web service )


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 2.x maven dependency configuration below. If you are new to maven refer our maven tutorials.

Jersey 2.x dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ehowtonow</groupId>
  <artifactId>RestTutorials</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestTutorials Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>
        <jersey2.version>2.19</jersey2.version>
        <jaxrs.version>2.0.1</jaxrs.version>
    </properties>
  <dependencies>
      <!-- JAX-RS -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${jaxrs.version}</version>
        </dependency>
        <!-- Jersey 2.19 -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>RestTutorials</finalName>
  </build>
</project>

HTTPHeaderParamService.java

package com.ehowtonow.webservice;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/tutorials")
public class HTTPHeaderParamService {

	@GET
	@Path("/java-tutorial")
	public Response getTutorial(@HeaderParam("user-agent") String userAgent) {
		return Response.status(200).entity("User Agent Details : " + userAgent).build();
	}
}

In this tutorials I used user-agent alone. You can access all http headers using @HeaderParam

Configure Jersey 2.x Servlet dispatcher

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Restful Webservice Example</display-name>
	<servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
			<param-value>com.ehowtonow.webservice</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

Now you can run the service and access the service by calling the following URL

http://localhost:8080/RestTutorials/rest/tutorials/java-tutorial

Output :

User Agent Details : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36


Ask your questions in eHowToNow Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Posts You may Like

  1. How to read all the files from folder using File.list() in Java
  2. Java Mail API – Introduction
  3. Java 8 Lambda Expression
  4. How to format the String by using Locale
  5. How to search a word inside String
  6. Restful Webservice HelloWorld Jersey 2.x Example
  7. How to check file existence in Java
  8. How to validate IPv4 Address with Port using Java Regular Expression
  9. JAX-RS Send List using QueryParam Annotation
  10. How to reverse the String using StringBuffer
  11. How to find last occurrence of substring inside string
  12. Java Best Practice – White Spaces
  13. Download Excel file using JAX-RS RESTful Webservice
  14. Java 7 Switch statement on String
  15. Java 8 forEach with Map Example

Be the first to comment

Leave a Reply

Your email address will not be published.


*