我正在尝试使用Spring
在http响应中启用缓存。这是我用来创建响应的代码。
@Controller
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class MessagesEndpoint {
MessagesService messagesService = new MessagesService();
@RequestMapping(value = "/messages/{messagesToken}", method = RequestMethod.GET)
public void getMessages(HttpServletResponse response) throws IOException {
Map<String, String> messages = messagesService.getMessages();
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
PrintWriter out = response.getWriter();
out.append("var initMessages = ");
out.append(mapper.writeValueAsString(messages));
out.close();
response.setHeader("Pragma", "Public");
response.setHeader("Cache-Control", "max-age=31556926");
}
}
以下是我的chrome dev工具的标题。
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Cookie: too long
Host:localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/donations/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
如您所见,缓存为no-cache
。为什么这些标题会被覆盖?