Spring控制器没有获得Cache Control标头

时间:2017-05-30 09:13:55

标签: spring spring-mvc spring-boot spring-security

我想在响应中设置缓存控制头中的最大年龄。我已经写了如下,但仍然有max-age 0.我想只为一个方法设置最大年龄,所以我不想禁用默认值。我应该是最好的。

 @ApiOperation(value = "get value by foreign currency", response = Property.class)
@RequestMapping(method = RequestMethod.GET, value = "/properties/{id}")
@ResponseBody
public ResponseEntity<BigDecimal> getValueByForeignCurrency(@PathVariable Long id,
                                                @RequestParam("currency") String currency, Locale locale) {
    if (!ForeignCurrency.isLegalCurrency(currency)) {
        throw new IllegalArgumentException("Currency: " + currency + " is not legal");
    }

    BigDecimal foreignValue = propertyService.getPropertyValueInForeignCurrency(id, currency, locale);

    return ResponseEntity.ok().cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS))
            .body(foreignValue);
}

Sombody知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

默认情况下,

SpringSecurity设置为no-cache模式。

您可以在HttpServletResponse对象中设置缓存设置。

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String welcome(HttpServletResponse response) { 
    response.setHeader("Cache-Control", "no-transform, public, max-age=3600"); 
    return "welcome"; 
}

请参阅this以获取官方文档。