JAX-RS : Binding of Request header using @HeaderParam in Jersey

Connect with

JAX-RS headerparam in Jersey Rest APIJAX-RS headerparam used for binding of request header values and retrieving of header param value in controller. In JAX-RS, binding of Header variable in the controller/service method is very easy by using @HeaderParam annotation.

1. Maven Dependency for Jersey JAX-RS headerparam

You can choose version of jersey as per your choice.


2. Binding with one header variable

package com.mysoftkey.jaxrs;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

 * This Jersey implementation of service class is used to demonstrate to get values from Request Header.
 * @author Ranjeet Jha
public class HeaderParamService {

	 * In this method , retrieving values from Request header.
	 * service end point:  http://localhost:8080/jersey/headerParam/get
	 * @param userAgent
	 * @return
	public Response getUserAgent(@HeaderParam("user-agent") String userAgent) {

		return Response.status(200).entity("User-Agent : " + userAgent).build();


Service URL: http://localhost:8080/jersey/headerParam/get
Output :

User-Agent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36

3. Get All Header Variable from Request Header

 * In this method , retrieving all available key/value from Request header.
 * service end point:  http://localhost:8080/jersey/headerParam/list
 * @param headers
 * @return
public Response getHeaders(@Context HttpHeaders headers) {

	Map headerMap = new HashMap();
	for (String header : headers.getRequestHeaders().keySet()) {
		headerMap.put(header, headers.getRequestHeader(header).get(0));
	return Response.status(200).entity(headerMap).build();

Service URL: http://localhost:8080/jersey/headerParam/list
Output :

connection: "keep-alive",
accept-language: "en-GB,en-US;q=0.8,en;q=0.6",
host: "localhost:8080",
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
user-agent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
accept-encoding: "gzip, deflate, sdch",
upgrade-insecure-requests: "1"

4.Download Source Code for JAX-RS Headerparam example

Download source code of JAX-RS: Jersey Request HeaderParam Example


You can find the JAX-RS tutorials for JAX-RS headerparam in Jersey rest API

  1. Jersey Official site
  2. JAX-RS official site
  3. Oracle Java Web services tutorial
  4. Wikipedia

Happy learning for this JAX-RS tutorial for retriving header param value from request header.

Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *