Setting up a jersey for caching?

I have the following @GET jersey method. It works fine, but always includes a No-cache header. I would like to allow the client to cache this data as it rarely changes.

ResponseBuilder rb = Response.ok(c);
    CacheControl cc = new CacheControl();
    cc.setMaxAge(60);
    cc.setNoCache(false);
    return rb.cacheControl(cc).build();

The answer is always:

 Server Apache-Coyote/1.1
 Pragma No-cache
 Cache-Control  no-cache, no-transform, max-age=60
 Expires    Wed, 31 Dec 1969 19:00:00 EST
 Content-Type   application/xml
 Content-Length 291
 Date   Tue, 16 Feb 2010 01:54:02 GMT

What am I doing wrong here?

+3
source share
2 answers

This was caused by the inclusion of BASIC auth.

Putting this in context will fix the problem:

<Valve className="org.apache.catalina.authenticator.BasicAuthenticator"
       disableProxyCaching="false" />

Hope this helps someone else.

+5
source

Your code looks fine.

Which container are you using? Make sure the cache is not disabled. Also check response handlers or filters that do not set the no-cache directive.

0
source

Source: https://habr.com/ru/post/1732943/


All Articles