While writing a RESTful web service, I am encountering issues if I enable any sort of caching on my client (currently a .NET thick client). By default Jersey is not sending any sort of cache control header, so the client is caching most pages automatically (which seems to be valid behaviour).
I would like to have Jersey by default send a cache control of “no-cache”, and then in particular responses override the cache control.
Is there any way to do this with Jersey?
I’ve found that RESTeasy has the ability to use the @NoCache annotation to specify the setting for the whole class, but I’ve not found anything similar with Jersey.
This is easy with Jersey by using a ResourceFilterFactory – you can create any custom annotation you attach to your methods to set cache control settings. ResourceFilterFactories get called for each discovered resource method when the application initializes – in your ResourceFilterFactory you can check if the method has your @CacheControlHeader annotation (or whatever you want to call it) – if not, simply return response filter that adds “no-cache” directive to the response, otherwise it should use the settings from the annotation. Here is an example of how to do that:
The annotation can look like this:
The ResourceFilterFactory can be registered in your application by adding the following init param to the definition of Jersey servlet in web.xml: