I’m maintaining an application that uses Jersey to create a RESTful API.
I want to set the response header on all of them to turn off caching.
I can do this by brute force replacing every
Response.ok().build()
with
Response.ok().cacheControl(noCache).build()
where noCache is defined earlier as
CacheControl noCache = new CacheControl();
noCache.setNoCache(true);
(and making a comparable change for all other Response objects I build) but making this change at all 100+ places that there is a return value seems ham-handed. Is there a simple way to set a preference for all responses that I produce?
Unless there’s a magic settings in Jersey to do that, one way I can think of is to use AOP library like AspectJ. You capture invocation of the build() method, and do the cacheControl before.
But whether this worth the effort compared to doing an eclipse ‘search all caller of this method and replace with something else’ is questionable.
An even better programming style according to DRY (Do-not Repeat Yourself) principle is to abstract & centralize the way response is built, such that when you need to change it you only have to change in one location, not multiple. Maybe you can apply service pattern here (eg: create a ResponseBuilderService).