JAX-RS @CookieParam CRUD Example
Cookies are used to set up a session between the client and server or to remember the identity and user preferences between requests. These cookie values are transmitted to and from the client and server via cookie headers. In this tutorial we show you how to do some basic CRUD operations with Cookies: Create, Read, Update and Delete.
Get HTTP Request Cookie
We can retrieve the entire cookie by injecting the Cookie
with the @CookieParam
JAX-RS annotation. By providing the cookie name.
@GET
public Response getCookie(@CookieParam("name") Cookie cookie){
return Response.ok().build();
}
We can also retrieve just the cookie value by injecting the value with the @CookieParam
annotation.
@GET
@Path("/value")
public Response getCookieValue(@CookieParam("name") String cookie){
return Response.ok().build();
}
Create New HTTP Response Cookie
To create a new HTTP response cookie we can simply add a new cookie via the ResponseBuilder
by calling the cookie()
method providing a NewCookie
object. This method will add a new cookie to the response.
@POST
public Response createCookie(){
return Response
.ok()
.cookie(new NewCookie("name", "value"))
.build();
}
Update Existing Cookie
If you want to update an existing cookie, you can simply add a new cookie with the same name. This will override the existing cookie.
@PUT
public Response updateCookie(@CookieParam("name") Cookie cookie){
if (cookie != null){
return Response
.ok()
.cookie(new NewCookie("name", "new-value"))
.build();
}
return Response.ok().build();
}
Delete Cookie
To delete an existing cookie you can set the maxAge property to 0. This will remove the cookie.
@DELETE
public Response deleteCookie(@CookieParam("name") Cookie cookie){
if (cookie != null){
NewCookie newCookie = new NewCookie(cookie, "delete cookie", 0, false);
return Response
.ok()
.cookie(newCookie)
.build();
}
return Response.ok().build();
}
Get List of All Cookies
You can retrieve a list of all cookies by injecting the HttpHeaders
with the @Context
annotation. From this object you can retrieve all the cookies from the HTTP Request.
@GET
public Response list(@Context HttpHeaders headers){
for (String name : headers.getCookies().keySet()) {
Cookie cookie = headers.getCookies().get(name);
System.out.println("Cookie: " + name + "=" + cookie.getValue());
}
return Response.ok().build();
}
Send Cookie with JAX-RS Client API
You can send cookies using the JAX-RS Client API by calling the Builder#cookie(name, value)
method. This will add a request cookie to the HTTP Request.
package com.memorynotfound.rs;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;
public class RunClient {
public static void main(String... args){
Client client = ClientBuilder.newClient();
Response response = client
.target("http://localhost:8081/jaxrs-form-param/api/courses")
.request()
.cookie("name", "value")
.get();
if (response.getStatus() == 200){
System.out.println("Response: " + response.readEntity(String.class));
}
client.close();
}
}