JAX-RS cache control finally became easy! Add Cache-Control
or Expires
headers to all your responses with a simple annotation.
To get started, add the following dependency from jitpack:
https://jitpack.io/#tobilarscheid/jaxrs-cache-control-filter/v-1.0.1
For Cache-Control, annotate your service Method with @CacheControlled
and pass all the @CacheControlDirectives
you want to use as an array.
@CacheControlled({@CacheControlDirective(name=MAX_AGE, value="100")})
public Response getMyEntity(){
MyEntity e = new MyEntity();
return Response.ok(e).build();
}
The Cache-Control
header is automatically added to your Response using JAX-RS CacheControl.
For Expires, annotate your service Method with @Expires
and pass a RFC 1123
Datetime describing when your resource expires.
@Expires("Tue, 3 Jun 2008 11:05:30 GMT")
public Response getMyEntity(){
MyEntity e = new MyEntity();
return Response.ok(e).build();
}
The Expires
header is automatically added to your Response.
- All the functionality is only applied if your ResponseĀ“s status code is
200
and the Request Method wasGET
. - If you don't use class path scanning (hint: you shouldn't), you need to list
de.tobiaslarscheid.cache.CacheControlFilter.class
and/orde.tobiaslarscheid.cache.ExpiresFilter.class
in your Rest Application. (Or whereever else you register your resource classes) - If you want to add ETags to complete your caching, you should have a look at JAX-RS ETag Filter
MIT