我有一个 jax-rs 端点。在端点内部,我需要删除或更改现有 cookie 的现有值。下面我提到了我的代码。
@GET
@Path("/")
@Produces("text/html")
public String logout(@Context HttpServletRequest request, @QueryParam("logoutNotification") String logoutNotification,
@QueryParam("id_token_hint") String id_token_hint,@CookieParam("statusCookie") javax.ws.rs.core.Cookie cookie) {
Response response=null;
if(logoutNotification.equals("T")) {
if (cookie != null) {
//update the value of the statusCookie cookie or remove the existing statusCookie cookie
}
}
请有人帮我实现这个。谢谢。
最佳答案
你可以尝试这样的事情。
@GET
@Path(value = "/test")
public Response test(@CookieParam("statusCookie") javax.ws.rs.core.Cookie cookie) {
NewCookie newCookie = null;
if(cookie != null) {
newCookie = new NewCookie("statusCookie", "second");
} else {
newCookie = new NewCookie("statusCookie", "first");
}
return Response.ok("test").cookie(newCookie).build();
}
第一次检查名为statusCookie
的cookie是否存在,如果不存在,则创建一个新cookie并将值设置为first
。当 statusCookie
存在时,它还会创建一个具有相同名称的新 cookie 并将值设置为 second
这实际上是覆盖现有的 cookie 值。因为同名。
关于java - 更改 cookie 的值或删除 jax-rs 端点内的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30779535/