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

Connect with

JAX-RS cookie exampleJAX-RS cookie example using @CookieParam annotation in Jersey rest API, for retrieving cookie parameter in Jersey rest API. 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 cookies still it’s handy to use in JAX-RS services. Whether you are preferring or not is not important, if it required 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 for JAX-RS cookie

You can choose version of jersey as per your choice for retrieving cookie value in JAX-RS cookie example in this JAX-RS tutorial.


   com.sun.jersey
   jersey-server
   1.9

2. Get specific key/value from Cookie

You can retieve the value from cookie using JAX-RS @CookieParam annotation.

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 endpoint:  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

Happy Learning 🙂 for learnign of JAX-RS cookie retrieval method.


Connect with

Leave a Comment

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