-1

I wanto set max age in cache control header in respon. I have write this as below, but still has max-age 0. i want to set max age only for one method, so i wantnot to disable default. i thik is should 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 know what i done wrong ?

kometen
  • 6,536
  • 6
  • 41
  • 51
RafalQA
  • 127
  • 2
  • 3
  • 12
  • 1
    `SpringSecurity`, by default sets to `no-cache` mode. You can set cache settings in `HttpServletResponse` object. `@RequestMapping(value = "/", method = RequestMethod.GET) public String welcome(HttpServletResponse response) { response.setHeader("Cache-Control", "no-transform, public, max-age=3600"); return "welcome"; }` – harshavmb May 30 '17 at 09:41
  • Thank you vaery much. I changed it to : 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); } and in response is Cache-Control →no-transform, public, max-age=3600 but I think it does'nt works. After reloaded count is increase. – RafalQA May 30 '17 at 10:29
  • okay. How are you validating the response cache headers? By having counter in a method and increment it? Ideally, you should check the browser network console and check the response headers. https://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome is the link – harshavmb May 30 '17 at 10:35
  • My response headers are :My Cache-Control:no-transform, public, max-age=3600 Content-Length:34 Content-Type:text/html;charset=UTF-8 Date:Tue, 30 May 2017 10:51:24 GMT Expires:0 Pragma:no-cache X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1; mode=block – RafalQA May 30 '17 at 10:53
  • and general: Status Code:200 deosn't has additional info that it is load from disc – RafalQA May 30 '17 at 10:56
  • If you see 3600 in the response headers, then your code is working. What exactly you want to achieve by adding the cache headers? You want browsers/clients to cache the content? – harshavmb May 30 '17 at 11:20
  • I want to browser not invoke another request to server after max age. Because currency value change only one as day. – RafalQA May 30 '17 at 14:27
  • So do you have aby idea ? – RafalQA May 31 '17 at 19:24
  • Refer https://stackoverflow.com/questions/31412918/spring-security-no-way-to-avoid-cache-control and https://stackoverflow.com/questions/24164014/how-to-enable-http-response-caching-in-spring-boot – harshavmb Jun 01 '17 at 02:52
  • 1
    We have set the cache headers, just disable `Pragma:no-cache` and it should work – harshavmb Jun 01 '17 at 02:53
  • harshavmb - thank you very much for help – RafalQA Jun 02 '17 at 11:30
  • Glad to now that it helped you! – harshavmb Jun 02 '17 at 11:31

1 Answers1

1

SpringSecurity by default sets to no-cache mode.

You can set cache settings in HttpServletResponse object.

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

Refer this for official documentation.

harshavmb
  • 3,404
  • 3
  • 21
  • 55
  • There is also Prgama - no-cache header, and it is default set too. If I disable cache in security config it will works, but i want to only for one controllers method. This is the problem now. Btw thank you very much for answer – RafalQA Jun 05 '17 at 06:53
  • okay `Pragma:no-cache` is being used for backward compatibility with HTTP 1.0 version. `Cache-Control` is used for HTTP1.1, `Pragma` header works in the absence of `Cache-Control` header. Yours is a bit tricky case as both are conflicting. – harshavmb Jun 05 '17 at 07:18
  • Well, can you try adding this `response.setHeader("Pragma", "cache");` to your controller and try – harshavmb Jun 05 '17 at 07:23