Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

http

spring

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?

like image 343
membersound Avatar asked Dec 05 '25 10:12

membersound


1 Answers

I guess you can implement the check of the last modified, Spring has support for that - ref API

Something like

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


    youeObject = service.find(id);
    ResponseBuilder builder = Response.ok(youeObject);
    builder.cacheControl(CacheControl.maxAge(24, TimeUnit.HOURS)).cachePrivate());
    return builder.build();

}
like image 185
Sheetal Mohan Sharma Avatar answered Dec 08 '25 00:12

Sheetal Mohan Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!