In an annotation-based Spring MVC controller, what is the preferred way to set cache headers for a specific path?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
org.springframework.web.servlet.support.WebContentGenerator, which is the base class for all Spring controllers has quite a few methods dealing with cache headers:
They can either be invoked within your controller prior to content generation or specified as bean properties in Spring context.
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:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
does not have the "cacheSeconds" property set.Add an instance of
WebContentInterceptor
: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:
See also: http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html
I found
WebContentInterceptor
to be the easiest way to go.You could extend AnnotationMethodHandlerAdapter to look for a custom cache control annotation and set the http headers accordingly.