JAX-RS : Get Cookie value using @CookieParam in Jersey

Connect with

Server can store state information in cookies on the client, and the same can be retrieved that information when the client makes its next request. Many web applications use cookies to set up a session between the client and the server. They also use cookies to remember identity and user preferences between requests. These cookie values are transmitted back and forth between the client and server via cookie headers.

Although, I do not prefer to use cookie but still it’s handy to use in JAX-RS services. Whether you are prefering or not is not the important, if it required to to solve our contextual problem it should be used. The @CookieParam annotation allows you to inject cookies sent by a client request into your JAX-RS resource methods.

1. Maven Dependency

You can choose version of jersey as per your choice.


   com.sun.jersey
   jersey-server
   1.9

2. Get specific key/value from Cookie

package com.mysoftkey.jaxrs;

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

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

/**
 * This service class is used to demonstrate cookie parameter using @CookieParam.
 * 
 * @author Ranjeet Jha 
 *
 */
@Path("cookieParam")
public class CookiesParamService {
	
	/**
	 * In this method , retrieving cookie values using @CookieParam.
	 * 
	 * service end point:  http://localhost:8080/jersey/cookieParam/get
	 * 
	 * @return
	 */
	@GET
	@Path("/get")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getCookie(@CookieParam("User-Agent") String userAgent,  @CookieParam("Last-Accessed") String LastAccessed) {

		Map cookieMap = new HashMap();
		cookieMap.put("Last-Accessed", LastAccessed);
		cookieMap.put("User-Agent", userAgent);
		return Response.status(200).entity(cookieMap).build();

	}
}

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

{
Last-Accessed: "1/3/16 1:11:48 PM IST",
User-Agent: "Mozilla/5.0 (Windows NT 6.1##semicolon## WOW64) AppleWebKit/537.36 (KHTML##comma## like Gecko) Chrome/47.0.2526.106 Safari/537.36"
}

3. Get List Key/value from Cookie programatically using @Context

	
/**
 * In this method , retrieving List of cookie values from Request header.
 * 
 * service end point:  http://localhost:8080/jersey/cookieParam/list
 * 
 * @return
 */
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public Response getCookieList(@Context HttpHeaders headers) {

	Map cookieMap = new HashMap();
	
	for (String key : headers.getCookies().keySet()) {
		Cookie cookie = headers.getCookies().get(key);	
		cookieMap.put(key, cookie.getValue());
	}
	return Response.status(200).entity(cookieMap).build();

}

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

{
Last-Accessed: "1/3/16 1:11:48 PM IST",
User-Agent: "Mozilla/5.0 (Windows NT 6.1##semicolon## WOW64) AppleWebKit/537.36 (KHTML##comma## like Gecko) Chrome/47.0.2526.106 Safari/537.36"
}

4.Download Source Code

Download source code of JAX-RS: Jersey CookieParam Example

5.References

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

Connect with

Leave a Reply

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