简体   繁体   English

响应中的Jax-rs缓存控制标头不起作用

[英]Jax-rs Cache control header in response does not work

I am trying to cache the response of rest service but at every subsequent request request is sent to server. 我正在尝试缓存其余服务的响应,但是在每个后​​续请求中,请求都被发送到服务器。 I am not sure what is I am missing in the code. 我不确定代码中缺少什么。 I am using JAX-RS for setting the cache headers. 我正在使用JAX-RS设置缓存头。 Initially I thought it is due to query parameter in the request, so i removed that but caching didn't work even after that. 最初,我认为这是由于请求中的查询参数所致,所以我删除了该参数,但是即使在此之后,缓存也无法正常工作。

This is what I have written: 这是我写的:

@GET
@Path("/primeNumber")
public Response prime() {
    String number = "3";
    boolean isPrime = true;
    int numberInt = Integer.valueOf(number);
    for (int i = 2; i <= numberInt/2 ; i++) {
        if (numberInt % i == 0) {
            isPrime = false;
            break;
        }
    }

    CacheControl cacheControl = new CacheControl();
    cacheControl.setMaxAge(60000);

    long current = new Date().getTime();
    Date expires = new Date(current + (100 * 1_000));

    return Response.status(Status.OK).entity(isPrime).expires(expires).cacheControl(cacheControl).build();
}

and my response header look like: 和我的响应头看起来像:

响应标题

I have tried with Chrome, Firefox and Advanced REST client(Chrome extension ) for testing. 我已经尝试过使用Chrome,Firefox和Advanced REST客户端(Chrome扩展程序)进行测试。

I was able to figure out the problem.Nothing wrong with the way I have implemented this but I was testing it by browser refresh and as mentioned here , refreshing the window will send a request to server. 我能找出problem.Nothing错我已经实现了这一点,但我被刷新浏览器测试,并为所提及的方式在这里 ,刷新窗口将发送到服务器的请求。 When I tested the same code via link navigation, it worked fine. 当我通过链接导航测试相同的代码时,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM