JBoss7将RESTEasy的所有响应的Cache-Control,Pragma设置为no-cache [英] JBoss7 setting Cache-Control, Pragma to no-cache for all responses from RESTEasy

查看:126
本文介绍了JBoss7将RESTEasy的所有响应的Cache-Control,Pragma设置为no-cache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用RESTEasy框架将Cache-Control标头添加到JBoss 7中生成的响应中.但是,由于默认情况下JBoss添加了no-cache头,因此所有响应最终都会获得多个Cache-Control头.

I'm trying to add Cache-Control headers to the responses generated in JBoss 7 using the RESTEasy framework. However, all the responses end up getting multiple Cache-Control headers due to JBoss adding a no-cache header by default.

我找不到要删除它的任何设置,并且添加拦截器也无法正常工作,因为稍后将添加无缓存标头.

I can't find any setting to remove it and adding interceptors is also not working since a no-cache header is being added later.

有人可以告诉我如何在JBoss 7中禁用默认的编译指示和缓存控制标头吗?

Can someone tell me how to disable the default pragma and cache-control headers in JBoss 7?

注意:我正在使用Resteasy与无状态EJB.

Note: I'm using resteasy with Stateless EJBs.

@Path("/api")
@Local
public interface UCSRestServiceInterface
{
    @GET
    @Path("/token")
    @Produces("application/json")
    @Cache(maxAge = 3600, noTransform = true)
    public Response getToken();
}

获取响应标头,

{
  "pragma": "No-cache",
  "date": "Thu, 11 Feb 2016 20:16:30 GMT",
  "content-encoding": "gzip",
  "server": "Apache-Coyote/1.1",
  "x-frame-options": "SAMEORIGIN",
  "vary": "Accept-Encoding,User-Agent",
  "content-type": "application/json",
  "cache-control": "no-cache, no-transform, max-age=3600",
  "transfer-encoding": "chunked",
  "connection": "Keep-Alive",
  "keep-alive": "timeout=15, max=100",
  "expires": "Wed, 31 Dec 1969 19:00:00 EST"
}

推荐答案

Web应用程序中的筛选器资源基本上可以让您拦截请求和响应,并且主要针对某些通过更改请求/响应标头起作用的用例而设计.更多详细信息此处

A Filter resource in a web-app basically lets you intercept requests and response and mainly designed for some use cases which work by altering request/response headers. More details here

由于您使用的是RESTEasy,因此可以使用ContainerResponseFilter; JAX-RS提供的过滤器接口.您可以通过实现此接口来编写自定义过滤器.过滤器类(将一个添加到您的Web应用程序源代码中)如下所示:-

Since you are using RESTEasy you can make use of ContainerResponseFilter; a filter interface provided by JAX-RS. You can write your custom filter by implementing this interface. The filter class(add one to your web app source code) will look like below:-

@Provider
public class YourCustomFilter implements ContainerResponseFilter{

// you can check the actual string value by using method "getStringHeaders" on 'resp' below
private static final String CACHE_CONTROL = "cache-control";

@Override
public void filter(ContainerRequestContext req,
        ContainerResponseContext resp) throws IOException {

    if(resp.getHeaders().containsKey(CACHE_CONTROL)){
       resp.getHeaders().remove(CACHE_CONTROL);
       resp.getHeaders().add(CACHE_CONTROL, "no-transform, max-age=3600");
    }
    resp.getHeaders().add(CACHE_CONTROL, "no-transform, max-age=3600");

  }

}

在这里,您基本上检查Cache-Control标头是否存在,并删除现有的并添加您自己的一个. 请不要忘记jax rs运行时需要的@Provider批注,以发现您的自定义过滤器.

Here you basically check for the prescense of Cache-Control header and if present remove the existing one and add one of your own. Please dont forget the @Provider annotation which is needed by the jax rs runtime to discover your custom filter.

请注意,使用此过滤器,对您Web应用程序的所有请求都将被拦截.如果您想拦截某些资源或资源方法,可以探索使用

Note that with this filter all requests to your webapp will be intercepted. If you want a certain resource or a resource method to be intercepted you can explore the use of @NameBinding

让我知道是否可行.

这篇关于JBoss7将RESTEasy的所有响应的Cache-Control,Pragma设置为no-cache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆