在控制器中设置Cache-Control、ETag和Last-Modified响应头
@RequestMapping("/book/{id}")
public ResponseEntity<Book> showBook(@PathVariable Long id) {
Book book = findBook(id);
String version = book.getVersion();
return ResponseEntity
.ok()
.cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
.eTag(version) // 这里也能操作最后修改时间lastModified,只不过没有一一展示
.body(book);
}@RequestMapping
public String myHandleMethod(WebRequest webRequest, Model model) {
long lastModified = // 1. 应用相关的方式计算得到(application-specific calculation)
if (request.checkNotModified(lastModified)) {
// 2. 快速退出 — 不需要更多处理了
return null;
}
// 3. 若资源更改了,那么再进行请求处理阶段,一般而言是准备响应内容
model.addAttribute(...);
return "myViewName";
}Last updated