简体   繁体   中英

How to get cookie in filter rest api jersey

I am sending cookie from a client in Rest API request and I want to read on a filter. I am using this code on the filter for retrieving the cookies.

Map<String, Cookie> cookies  = requestContext.getCookies();
Cookie cookie = cookies.get("token");
String token = cookie.getValue();

But it returns null. Can anyone suggest me how can I retrive cookie on filter.

In order to retrieve your cookie, you should do the following:

for (Cookie c : requestContext.getCookies().values()) 
{
    if (c.getName().equals("token")) {
        cookie = c;
        break;
    }
}

Honestly, I don't know why you could not retrieve your cookie by its key, since the ContainerRequestContext documentation states that getCookies returns a read-only map of cookie name (String) to Cookie.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM