JAX-RS @HeaderParam Annotation Example with RESTEasy 3

By Arvind Rai, March 07, 2015
In this page we will learn JAX-RS @HeaderParam annotation with RESTEasy 3. Request headers can be accessed easily by passing the header name to @HeaderParam annotation in a method argument. @HeaderParam can be used with String datatype or user defined class. If we are using @HeaderParam with user defined class, we need to create a constructor with one argument of string data type or create a static method with one argument of string datatype that name can be valueOf or fromString.

@HeaderParam with String Parameter

We can fetch header parameters using String argument annotated with @HeaderParam .
@GET
public Response oneWay(@HeaderParam("User-Agent") String userAgent){} 

@HeaderParam with Class Having Constructor with One String Argument

Create a class with one argument constructor. Datatype should be String.
UserDataOne.java
package com.concretepage;
public class UserDataOne {
    private String userAgent;	
	public UserDataOne(String userAgent){
	  this.userAgent = userAgent;
	}
	public String getUserAgent() {
		return userAgent;
	}
	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}
} 
We can fetch header in this class passing it as argument
@GET
public Response secondWay(@HeaderParam("User-Agent") UserDataOne data){} 

@HeaderParam with Class Having static Method with Name valueOf or fromString

Another way is create a class with the static method name valueOf or fromString with one argument of String datatype.
UserDataTwo.java
package com.concretepage;
public class UserDataTwo {
    private static String data;	
       public static String getUserAgent() {
       return data;
    }
    public static void  valueOf(String userAgent){
      data = userAgent;
    }
} 
Use this class as below.
@GET
public Response thirdWay(@HeaderParam("User-Agent") UserDataTwo data){} 

Service Class using @HeaderParam Annotation

Find the service class as an example which has three method for the demo of each case discussed above.
EmployeeService.java
package com.concretepage;
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.Response;
import org.jboss.resteasy.annotations.providers.jackson.Formatted;
@Path("/header")
public class EmployeeService {
    @GET
    @Path("/oneway")
    @Produces("application/json")
    @Formatted
    public Response oneWay(@HeaderParam("User-Agent") String userAgent,
    		                @HeaderParam("Accept") String accept,
    		                @HeaderParam("Accept-Encoding") String encoding,
    		                @HeaderParam("Accept-Language") String lang) {
    	
    	Map map = new HashMap();
    	map.put("User-Agent", userAgent);
    	map.put("Accept", accept);
    	map.put("Accept-Encoding", encoding);
    	map.put("Accept-Language", lang);
        return Response.ok(map).build();
    }
    @GET
    @Path("/secondway")
    @Produces("application/json")
    @Formatted
    public Response secondWay(@HeaderParam("User-Agent") UserDataOne data) {
        return Response.ok(data).build();
    }
    @GET
    @Path("/thirdway")
    public Response thirdWay(@HeaderParam("User-Agent") UserDataTwo data) {
        return Response.ok(UserDataTwo.getUserAgent()).build();
    }
} 

Output

Find the output for different URL.
1. URL: http://localhost:8080/concretepage-1/employee/header/oneway
{
  "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  "User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0",
  "Accept-Encoding" : "gzip, deflate",
  "Accept-Language" : "en-US,en;q=0.5"
} 
2. URL: http://localhost:8080/concretepage-1/employee/header/secondway
{
  "userAgent" : "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"
} 
3. URL: http://localhost:8080/concretepage-1/employee/header/thirdway
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us