Home  >  Web Services  >  RESTEasy 3

JAX-RS RESTEasy 3 @Cache and @NoCache Annotations Example for Cache-Control

By Arvind Rai, March 07, 2015
To control cache RESTEasy 3 provides annotations as @Cache and @NoCache following JAX-RS specifications with @GET annotation. If any other request like POST, PUT etc is hit for that URL, then the cache is automatically invalidated. In server response, Cache-Control header is set using these annotations. We can set cache as must-revalidate, no-store, proxy-revalidate and max-age etc by using @Cache annotation. We can set completely no cache to response header by using @NoCache annotation. These annotations can be applied at class level and method level. Suppose if we apply @Cache at class level, it means all methods by default will serve as @Cache do. Any method can override the class level @Cache by using @NoCache. In the same way class level @NoCache can be applied and can be overridden by @Cache annotation. These annotation are applied with @GET annotation. Here in this page we will discuss RESTEasy @Cache and @NoCache annotations by example.

@Cache

RESTEasy provides @Cache annotation that automatically sets Cache-Control . This annotation can be applied at class level and method level. Find the description of @Cache attributes . We must use @Cache with @GET JAX-RS annotation.

maxAge: Maximum time that an object will be considered to be fresh.
sMaxAge: Same as maxAge but it applies for proxy cache.
noStore: To prevent not to store sensitive information.
mustRevalidate: If true, we are saying to follow strictly to serve fresh object.
proxyRevalidate: Same as mustRevalidate but it applies for proxy cache.
isPrivate: If true, then response messages will be cached for a single user only and will not be shared. If false, it means response messages can be cached by any cache.

Find the code snippet for @Cache.
@GET
@Path("cache/{id}")
@Produces("application/json")
@Cache(maxAge=2300, mustRevalidate = true, noStore = true, proxyRevalidate = true, sMaxAge = 2300)
public Response cacheData(@PathParam("id") String id) {} 



@NoCache

@NoCache is used with @GET annotation. @NoCache annotation sets header as Cache-Control:no-cache and it means that, do not serve cache copy and get the fresh response from the origin server. @NoCache can be applied at class level and method level. If applied at class level, all methods will be served as no cache. Find the code snippet to apply at method level.
@GET
@Path("nocache/{id}")
@Produces("application/json")
@NoCache
public Response noCacheData(@PathParam("id") String id) {} 

Gradle File to Resolve RESTEasy API

Find the Gradle file to resolve RESTEasy API.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
        mavenCentral()
}
dependencies {
	compile 'org.jboss.resteasy:resteasy-jaxrs:3.0.10.Final'
	compile 'org.jboss.resteasy:resteasy-jackson2-provider:3.0.10.Final'
	compile 'org.jboss.resteasy:resteasy-cache-core:3.0.10.Final'
}  

Service Class

Find the service class which has two methods. One method will serve for Cache that will set attributes of cache control header and second method will serve to set no cache in cache control header.
EmployeeService.java
package com.concretepage;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.annotations.cache.Cache;
import org.jboss.resteasy.annotations.cache.NoCache;
@Path("/info")
public class EmployeeService {
    @GET
    @Path("nocache/{id}")
    @Produces("application/json")
    @NoCache
    public Response noCacheData(@PathParam("id") String id) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("id", id);
        map.put("msg", "Data Will Not be cached");
        return Response.ok(map).build();
    }
    @GET
    @Path("cache/{id}")
    @Produces("application/json")
    @Cache(maxAge=2300, mustRevalidate = true, noStore = true, proxyRevalidate = true, sMaxAge = 2300)
    public Response cacheData(@PathParam("id") String id) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("id", id);
        map.put("Msg", "Data Will be cached");
        return Response.ok(map).build();
    }
} 

Output

We have two URLs. One URL is showing @Cache demo and second URL is showing @NoCache demo. Find the URL for @Cache demo.
http://localhost:8080/concretepage-1/employee/info/cache/50
JAX-RS RESTEasy 3 @Cache and @NoCache Annotations Example for Cache-Control
Find the URL for @NoCache demo.
http://localhost:8080/concretepage-1/employee/info/nocache/56
JAX-RS RESTEasy 3 @Cache and @NoCache Annotations Example for Cache-Control

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us