Twitter

luni, 12 octombrie 2015

JAX-RS Access HTTP Cookie in Resource

In this post, you can see how to access cookie parameters from a JAX-RS resource

Let's suppose that we have an application named, JaxrsCookieParameters_EE7, and a cookie as: hellofrom = Leonard. Via javax.ws.rs.CookieParam we can easily access this cookie in a JAX-RS resource as below:
import javax.ws.rs.CookieParam;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("hello")
public class HelloResource {

    @GET
    @Produces("text/plain") // default: */*
    public String hello(@CookieParam(value="hellofrom") String from) {
        return "Hello, " + from+"!";
    }
}
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("webresources")
public class ApplicationConfig extends Application {
}
The output will be as in figure below (for testing I just added the cookie manually in Google Chrome). The test URL is: http://localhost:8080/JaxrsCookieParameters_EE7/webresources/hello


Note Do not forget that cookies implies several limitations (use of semi-colons ;, and commas ,)! Is important to read the corresponding RFC.

The complete application is available here.

Niciun comentariu:

Trimiteți un comentariu