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
The answer is quite simple:
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.you can define a anotation for this:
@CacheControl(isPublic = true, maxAge = 300, sMaxAge = 300)
, then render this anotation to HTTP Header with Spring MVC interceptor. or do it dynamic:Implication can be found here: 优雅的Builder模式
BTW: I just found that Spring MVC has build-in support for cache control: Google WebContentInterceptor or CacheControlHandlerInterceptor or CacheControl, you will find it.
In your controller, you can set response headers directly.
You could use a Handler Interceptor and use the postHandle method provided by it:
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/HandlerInterceptor.html
then just add a header as follows in the method:
Starting with Spring 4.2 you can do this:
CacheControl
object is a builder with many configuration options, see JavaDocI know this is a really old one, but those who are googling, this might help: