Gar*_*ary 12 jboss jax-rs cache-control resteasy jboss7.x
我正在尝试使用RESTEasy框架将Cache-Control标头添加到JBoss 7中生成的响应中.但是,由于JBoss默认添加了no-cache标头,所有响应最终都会获得多个Cache-Control标头.
我找不到任何设置来删除它,并且添加拦截器也无法正常工作,因为稍后会添加no-cache标头.
有人能告诉我如何在JBoss 7中禁用默认的pragma和缓存控制头吗?
注意:我正在使用无状态EJB的resteasy.
@Path("/api")
@Local
public interface UCSRestServiceInterface
{
@GET
@Path("/token")
@Produces("application/json")
@Cache(maxAge = 3600, noTransform = true)
public Response getToken();
}
Run Code Online (Sandbox Code Playgroud)
获取响应标头为,
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
Web 应用程序中的过滤器资源基本上可以让您拦截请求和响应,主要针对某些通过更改请求/响应标头来工作的用例而设计。更多详情请点击这里
由于您使用的是 RESTEasy,因此您可以使用 ContainerResponseFilter;JAX-RS 提供的过滤器接口。您可以通过实现此接口来编写自定义过滤器。过滤器类(将其添加到您的网络应用程序源代码中)将如下所示:-
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,您基本上检查 Cache-Control 标头是否存在,如果存在,则删除现有标头并添加您自己的标头之一。请不要忘记@Providerjax rs 运行时发现您的自定义过滤器所需的注释。
请注意,使用此过滤器,所有对您的 web 应用程序的请求都将被拦截。如果你想拦截某个资源或某个资源方法,可以探索@NameBinding的使用
让我知道这是否有效。
| 归档时间: |
|
| 查看次数: |
2755 次 |
| 最近记录: |