java - I want to add a header to Http Response, based on the Http Status code in Spring MVC -


i have add cache-control headers rest api designed in spring mvc, based on http response code. if response code 200, add header else dont add.

i dont want client cache response, in case not 200.

it's not possible in filters/interceptors, response committed controller, can't change response state.

is there other way add header after controller?

you can extend org.springframework.web.filter.onceperrequestfilter add cache-control header.

public class cachecontrolheaderfilter extends onceperrequestfilter {     @override     protected void dofilterinternal(httpservletrequest request,                                httpservletresponse response, filterchain filterchain) {         // add header here based on response code     } } 

declare filter spring bean in configuration.

<bean id="cachecontrolheaderfilter" class="*.*.cachecontrolheaderfilter" /> 

plugin filter in web.xml:

<filter>     <filter-name>cachecontrolheaderfilter</filter-name>     <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> </filter>  <filter-mapping>     <filter-name>cachecontrolheaderfilter</filter-name>     <url-pattern>/*</url-pattern> </filter-mapping> 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -