5 minutes with – Cookie in Jersey

This week I’ve had to use the cookies inside a RESTful service made by Jersey.

That’s not very complicated, you must only remember the execution flow at jersey request to make a good work. In this post I’d like to describe this flow sending a cookie in every response and read it again.

A Jersey servlet container define three steps during life-cycle requests.

The first step is defined by the interface ContainerRequestFilters.

package com.sun.jersey.spi.container;

public interface ContainerRequestFilter {

    public ContainerRequest filter(ContainerRequest cr);

Every requests go through this method before reach the request mapping.

public JSONWithPadding getlastLocalVisited(@CookieParam(value = "localLastVisited") String localLastVisited){
    return new JSONWithPadding(localLastVisited, callback);

The last step is calling ContainerResponseFilter interface.

package com.sun.jersey.spi.container;

public interface ContainerResponseFilter {

    public ContainerResponse filter(ContainerRequest cr, ContainerResponse cr1);

Suppose that you want to hold tracking of last article view by user. You should insert the code, like below, inside a class which implements ContainerResponseFilter.

public ContainerResponse filter(ContainerRequest request, ContainerResponse response)
    String item = request.getQueryParameters().getFirst("item");
    javax.ws.rs.core.NewCookie lastVisited = new javax.ws.rs.core.NewCookie("lastVisited",      
            "no comment",      
            1073741823 , // maxAge max int value/2      
            false );
    javax.ws.rs.core.Response cookieResponse = Response.fromResponse(response.getResponse()).cookie(lastVisited).build();


    return response;

In this way, every single requests that come to the web application will be tracking inside the filter method. Once create the cookie, it will be inserted into the Response.

The url /lastLocalVisited is used to read cookie value and get a Json string of it. Take a look at @CookieParam annotation.

At last, the web.xml file.








That’s it. Easy, fast and clear to apply. Sometimes I see codes like this to set a cookie:

response.getHttpHeaders().add("Set-Cookie", New Cookie(...));

Honestly, I don’t like it and my suggestion is to use the first method and avoid home-made solutions.


2 thoughts on “5 minutes with – Cookie in Jersey

  1. I followed what you say, however I ended up using the getHttpHeaders().add() approach.

    The problem with Response.fromResponse is that “hides” my classes.
    I have a MessageBodyWriter for my classes, if I do the fromResponse, the classes passed to my MessageBodyWriter are changed. I suppose this is, as the Javadoc say, because the new builder has its own metadata map.

    Fex: If I return List objects I get ArrayList in the MessageBodyWriter.
    If I just add the cookies to the response headers without the fromResponse I get List, that is what I need there.

    1. I agree with your example. Unfortunately, in some cases is impossible using the solution that I’ve illustrated in the article. Thanks for sharing your experience, I’ll test that as soon as possible.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.