membersound membersound - 3 months ago 21
HTTP Question

How to add cache-control to a @RestController servlet path?

I have a

servlet
to offer images resources via
spring
:

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/thumbnails", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> getThumbnail(int id) {
return service.find(id);
}


Resulting http headers:
Cache-control: "max-age=0"
.

Question: how can I control the cache header just for that method?

Answer

I ended as follows, which worked:

ResponseEntity.ok().lastModified(file.lastModified()).header("max-age", "86400");

I don't now why the approach with CacheControl class does not work.