Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set header no cache in spring mvc 3 by annotation

how to set header no cache in spring mvc 3 by annotation? not is

  response.setHeader("Pragma","No-cache");        response.setHeader("Cache-Control","no-cache");        response.setDateHeader("Expires",   0);      
like image 763
EdwardLau Avatar asked Dec 06 '10 08:12

EdwardLau


People also ask

What does -- no cache do?

Cache-control: no-cache The no-cache directive uses the ETag header field for validation of the cached response by making a roundtrip to and from the server to ensure that the response has not changed.

What is Cache-Control must revalidate?

The must-revalidate response directive indicates that the response can be stored in caches and can be reused while fresh. If the response becomes stale, it must be validated with the origin server before reuse. Typically, must-revalidate is used with max-age . Cache-Control: max-age=604800, must-revalidate.

What is cache-control private?

Cache-Control: PrivateThe private response directive indicates that a resource is user specific—it can still be cached, but only on a client device. For example, a web page response marked as private can be cached by a desktop browser, but not a content delivery network (CDN).

What is Cache-Control in Spring boot?

public class CacheControl extends Object. A builder for creating "Cache-Control" HTTP response headers. Adding Cache-Control directives to HTTP responses can significantly improve the client experience when interacting with a web application.


2 Answers

There is no such option. You can use an interceptor:

<mvc:annotation-driven/> <mvc:interceptors>     <bean id="webContentInterceptor"            class="org.springframework.web.servlet.mvc.WebContentInterceptor">         <property name="cacheSeconds" value="0"/>         <property name="useExpiresHeader" value="true"/>         <property name="useCacheControlHeader" value="true"/>         <property name="useCacheControlNoStore" value="true"/>     </bean> </mvc:interceptors> 

(taken from here)

On one hand it is logical not to have such annotation. Annotations on spring-mvc methods are primarily to let the container decide which method to invoke (limiting it by a request header, request url, or method). Controlling the response does not fall into this category.

On the other hand - yes, it will be handy to have these, because when controllers are unit-tested it is not relevant to test http header stuff (or is it?). And there are @ResponseBody and @ResponseStatus, which do specify some response properties.

like image 63
Bozho Avatar answered Oct 11 '22 13:10

Bozho


To override the settings for certain controller mappings use the cacheMappings properties object on the WebContentInterceptor

<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"> <property name="cacheSeconds" value="2100" /> <property name="useExpiresHeader" value="true" /> <property name="useCacheControlHeader" value="true" /> <property name="useCacheControlNoStore" value="true" /> <property name="cacheMappings">     <props>         <prop key="/myUncachedController">0</prop>     </props> </property> 

like image 44
Risadinha Avatar answered Oct 11 '22 13:10

Risadinha