Search code examples
javajax-rscache-control

CacheControl JAX-RS public directive


I try to set the public directive whith the javax.ws.rs.core.CacheControl class (JAX-RS)

For example : Cache-Control : public, max-age= 1000

But this piece of code doesn't create the public directive :

Response.ResponseBuilder rb = Response.ok(entity);

CacheControl cacheControl = new CacheControl();
cacheControl.setPrivate(false);
cacheControl.setMaxAge(1000);

rb.cacheControl(cacheControl);
return rb.build();

Solution

  • Setting private to false via cacheControl.setPrivate(false) does not mean that it should be cached public. There's also a no-cache token. As CacheControl has no method for setting the public token you need to do it manually:

    CacheControl cacheControl = CacheControl.valueOf("public, max-age=1000")