如何使用JAX-RS NewCookie删除服务器上的cookie?

我想删除服务器上的cookie(通过将Expires设置为过去)。 我怎么能用javax.ws.rs.core.NewCookie做到这一点? 我正在尝试这个,但它不起作用:

 return Response.ok() .entity("hello world!") .cookie( new NewCookie( "foo", "", "/", ".example.com", 1, "no comment", 0, // maxAge false ) ) .build(); 

此代码段生成此HTTP标头:

 Set-Cookie:foo=;Version=1;Comment="no comment";Domain=.example.com;Path=/ 

此标头不会从服务器中删除cookie。 什么是可能的解决方法?

这是它的工作原理(相当脏的方法):

 return Response.ok() .header( "Set-Cookie", "foo=deleted;Domain=.example.com;Path=/;Expires=Thu, 01-Jan-1970 00:00:01 GMT" ); 

我不能尝试提议,但它应该工作(因为它是java servlet API删除cookie的常见工作)。

步骤1.访问HttpServletResponse。 为此,在您的服务中声明如下:

 @Context HttpServletResponse _currentResponse; 

步骤2.让客户端通过设置到期时间来删除cookie

 Cookie userCookie = new Cookie(cookieName, ""); _currentResponse.setContentType("text/html"); userCookie.setMaxAge(0); _currentResponse.addCookie(userCookie);