2017-05-30 97 views
-1

我想在高速缓存控制头设置最大年龄。我写下如下,但仍然有最大年龄0.我想设置最大年龄只有一种方法,所以我不想禁用默认值。我thik应该ovveride。弹簧控制器没有得到高速缓存控制头

@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

'SpringSecurity'默认设置为'no-cache'模式。您可以在'HttpServletResponse'对象中设置缓存设置。 '@RequestMapping(值= “/”,方法= RequestMethod.GET) \t公共字符串欢迎(HttpServletResponse的响应){ \t \t response.setHeader( “缓存控制”,“无变换,公共,最大年龄= 3600" ); \t \t返回“欢迎”; \t}' – harshavmb

+0

谢谢vaery了。我将它改为: httpServletResponse.setHeader(“Cache-Control”,“no-transform,public,max-age = 3600”); count + = 1; return“Ale ma kota a kot ma ale a count”+ String.valueOf(count); },并作为响应缓存控制→无变换,公共,最大年龄= 3600,但我认为它不起作用。重载次数增加后。 – RafalQA

+0

好的。你如何验证响应缓存头?通过在一个方法中计数并增加它?理想情况下,您应该检查浏览器网络控制台并检查响应头。 https://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome是链接 – harshavmb

回答

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获取官方文档。

+0

还有Prgama - no-cache标头,它也是默认设置。如果我在安全配置中禁用缓存,它会起作用,但我只想要一个控制器方法。这是现在的问题。顺便说一句,非常感谢你回答 – RafalQA

+0

没问题'Pragma:no-cache'正被用于向后兼容HTTP 1.0版本。 'Cache-Control'用于HTTP1.1,'Pragma'头在没有'Cache-Control'头的情况下工作。你们有点棘手,因为两者都有冲突。 – harshavmb

+0

那么,你可以尝试添加这个'response.setHeader(“Pragma”,“cache”);'到你的控制器并尝试 – harshavmb