[JAX-RS] 3 ways to get cookies

These are 3 ways to obtain cookies values in JAX-RS

package com.example;

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

public class CookieResource {

	public String get(@CookieParam("customerId") int custId) {
		return "Customer ID was " + custId;

	public String getAnother(@CookieParam("customerId") Cookie customerId) {
		if (customerId != null) {
			return customerId.getName();
		return "no cookie named customerId";

	public String getHeaders(@Context HttpHeaders headers) {
		StringBuilder sb = new StringBuilder();
		for (String name : headers.getCookies().keySet()) {
			sb.append(name + " ");
		return sb.toString();


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s