I have to add Cache-Control headers to a rest API designed in Spring MVC, based on the Http Response code. If response code is 200, add the header else dont add.
I dont want the client to cache the response, in case it is not 200.
It's not possible in filters/interceptors, as the response is already committed from controller, so can't change response state.
Is there any other way to add header after controller?
You can extend
org.springframework.web.filter.OncePerRequestFilter
to addcache-control
header.Declare this filter as a spring bean in your configuration.
Plugin the filter in web.xml: