Change the value of the cookie or remove the cookie inside a jax-rs endpoint

1.3k Views Asked by At

I have a jax-rs endpoint. In side the endpoint I need to remove or change the existing value of an existing cookie. below I have mentioned my code.

@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
        }
        }

Somebody please help me to implement this. Thanks.

1

There are 1 best solutions below

2
On

you can try something like this.

@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();
}

first time check is cookie named statusCookie is exist if no it create a new cookie and set value to first. when statusCookie exist it also create a new cookie with same name and set value to second which is actually overwrite existing cookie value. because of same name.