Jersey: Can I add a cookie in ContainerResponseFilter?

前端 未结 1 318
攒了一身酷
攒了一身酷 2021-01-11 15:40

I have a ContainerResponseFilter and I tried to set a cookie in it as follows:

@Override
public void filter(ContainerRequestContext containerReq         


        
相关标签:
1条回答
  • 2021-01-11 16:08

    Yeah seems a bit odd that this is not allowed. Not sure why this is not allowed. I've tested both with Jersey and Resteasy. With Resteasy, all that happens is that the cookie is not set (no Exception). I'm thinking some operation sets the cookies as headers, and by the time the filter is reached, no further operation is done with the cookies.

    The only work around I can think of is to simply set the header yourself

    responseContext.getHeaders().add("Set-Cookie", new NewCookie("Hello", "World"));
    

    The toString() of NewCookie will return how it should look in the header

    Set-Cookie: Hello=World;Version=1
    
    0 讨论(0)
提交回复
热议问题