Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set cache headers in Spring MVC?

In an annotation-based Spring MVC controller, what is the preferred way to set cache headers for a specific path?

like image 477
D. Wroblewski Avatar asked Sep 01 '09 14:09

D. Wroblewski


People also ask

How do I set up Cache-Control headers?

To use Cache-Control headers, choose Content Management | Cache Control Directives in the administration server. Then, using the Resource Picker, choose the directory where you want to set the headers. After setting the headers, click 'OK'.

What is Cache-Control in Spring boot?

By using cache control headers effectively, we can instruct our browser to cache resources and avoid network hops. This decreases latency, and also the load on our server. By default, Spring Security sets specific cache control header values for us, without us having to configure anything.

What are the Cache-Control headers?

What is the Cache-Control Header. Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).


2 Answers

I just encountered the same problem, and found a good solution already provided by the framework. The org.springframework.web.servlet.mvc.WebContentInterceptor class allows you to define default caching behaviour, plus path-specific overrides (with the same path-matcher behaviour used elsewhere). The steps for me were:

  1. Ensure my instance of org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter does not have the "cacheSeconds" property set.
  2. Add an instance of WebContentInterceptor:

    <mvc:interceptors> ... <bean class="org.springframework.web.servlet.mvc.WebContentInterceptor" p:cacheSeconds="0" p:alwaysUseFullPath="true" >     <property name="cacheMappings">         <props>             <!-- cache for one month -->             <prop key="/cache/me/**">2592000</prop>             <!-- don't set cache headers -->             <prop key="/cache/agnostic/**">-1</prop>         </props>     </property> </bean> ... </mvc:interceptors> 

After these changes, responses under /foo included headers to discourage caching, responses under /cache/me included headers to encourage caching, and responses under /cache/agnostic included no cache-related headers.


If using a pure Java configuration:

@EnableWebMvc @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter {   /* Time, in seconds, to have the browser cache static resources (one week). */   private static final int BROWSER_CACHE_CONTROL = 604800;    @Override   public void addResourceHandlers(ResourceHandlerRegistry registry) {     registry      .addResourceHandler("/images/**")      .addResourceLocations("/images/")      .setCachePeriod(BROWSER_CACHE_CONTROL);   } } 

See also: http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html

like image 175
Eric R. Rath Avatar answered Sep 28 '22 11:09

Eric R. Rath


The answer is quite simple:

@Controller public class EmployeeController {
@RequestMapping(value = "/find/employer/{employerId}", method = RequestMethod.GET) public List getEmployees(@PathVariable("employerId") Long employerId, final HttpServletResponse response) { response.setHeader("Cache-Control", "no-cache"); return employeeService.findEmployeesForEmployer(employerId); }
}
Code above shows exactly what you want to achive. You have to do two things. Add "final HttpServletResponse response" as your parameter. And then set header Cache-Control to no-cache.
like image 24
goroncy Avatar answered Sep 28 '22 13:09

goroncy