Apache camel 如何在Apache Camel中设置缓存控制头

Apache camel 如何在Apache Camel中设置缓存控制头,apache-camel,cache-control,Apache Camel,Cache Control,我使用的是ApacheCamel 2.15.1版本。在本文中,我将servlet组件用于RESTDSL。我的简单路线如下所示 from(rest:get:CustomerDetails.json) .至(”http://localhost:8080/customer/getCustomerDetails?bridgeEndpoint=true"); 我需要为响应设置缓存控制和Pragma头 from(rest:get:CustomerDetails.json) .至(”http://localh

我使用的是ApacheCamel 2.15.1版本。在本文中,我将servlet组件用于RESTDSL。我的简单路线如下所示

from(rest:get:CustomerDetails.json)
.至(”http://localhost:8080/customer/getCustomerDetails?bridgeEndpoint=true");

我需要为响应设置缓存控制和Pragma头

from(rest:get:CustomerDetails.json)
.至(”http://localhost:8080/customer/getCustomerDetails?bridgeEndpoint=true")
.setHeader(“缓存控制”,常量(“私有,最大年龄=0,无存储”)

但骆驼忽略了这一点。我很少读到其他博客建议使用定制的HeaderFilterStrategy。我也试过这个。这没用


非常感谢您为解决此问题提供的任何帮助。

您可以将其与自定义HeaderFilterStrategy一起使用。诀窍是在restConfiguration()中配置它。endpointProperties(..)如下所示:

public void configure(){
JndiRegistry注册表=getContext().getRegistry(JndiRegistry.class);
bind(“filter”,newheaderfilter());
restConfiguration()
.host(“本地主机”)
.endpointProperty(“headerFilterStrategy”,“#filter”)
.setPort(“10000”);
from(“rest:get:hello”)
.至(”http://localhost:20000?bridgeEndpoint=true")
.setHeader(“缓存控制”,常量(“私有,最大年龄=0,无存储”);
来自(“netty http:http://localhost:20000")
.setBody(常数(“ok”));
}
其中,#filter就是这样的虚拟实现(您可以创建一个更适合您需要的过滤器)

公共类HeaderFilter实现HeaderFilterStrategy{
@凌驾
公共布尔applyFilterToCamelHeaders(字符串arg0、对象arg1、交换arg2){
//TODO自动生成的方法存根
返回false;
}
@凌驾
公共布尔applyFilterToExternalHeaders(字符串arg0、对象arg1、交换arg2){
//TODO自动生成的方法存根
返回false;
}
}

现在,如果不使用.endpointProperty(“headerFilterStrategy”,“#filter”)行运行我的routes,我会得到如下输出

$ curl -D - http://localhost:10000/hello
HTTP/1.1 200 OK
Accept: */*
breadcrumbId: ID-myhost-40508-1441899753215-0-1
User-Agent: curl/7.35.0
Content-Length: 2
Connection: keep-alive

ok
$ curl -D - http://localhost:10000/hello
HTTP/1.1 200 OK
Accept: */*
breadcrumbId: ID-myhost-56308-1441899833287-0-1
Cache-Control: private, max-age=0,no-store
CamelHttpMethod: GET
CamelHttpResponseCode: 200
CamelHttpUri: /hello
CamelHttpUrl: http://localhost:10000/hello
CamelNettyChannelHandlerContext: org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext@1fac34b
CamelNettyLocalAddress: /127.0.0.1:10000
CamelNettyMessageEvent: [id: 0x93dfe147, /127.0.0.1:35302 => /127.0.0.1:10000] RECEIVED: DefaultHttpRequest(chunked: false) GET /hello HTTP/1.1 User-Agent: curl/7.35.0 Host: localhost:10000 Accept: */*
CamelNettyRemoteAddress: /127.0.0.1:35302
User-Agent: curl/7.35.0
Content-Length: 2
Connection: keep-alive

ok
.endpointProperty(“headerFilterStrategy”,“#filter”)行输出,如下所示

$ curl -D - http://localhost:10000/hello
HTTP/1.1 200 OK
Accept: */*
breadcrumbId: ID-myhost-40508-1441899753215-0-1
User-Agent: curl/7.35.0
Content-Length: 2
Connection: keep-alive

ok
$ curl -D - http://localhost:10000/hello
HTTP/1.1 200 OK
Accept: */*
breadcrumbId: ID-myhost-56308-1441899833287-0-1
Cache-Control: private, max-age=0,no-store
CamelHttpMethod: GET
CamelHttpResponseCode: 200
CamelHttpUri: /hello
CamelHttpUrl: http://localhost:10000/hello
CamelNettyChannelHandlerContext: org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext@1fac34b
CamelNettyLocalAddress: /127.0.0.1:10000
CamelNettyMessageEvent: [id: 0x93dfe147, /127.0.0.1:35302 => /127.0.0.1:10000] RECEIVED: DefaultHttpRequest(chunked: false) GET /hello HTTP/1.1 User-Agent: curl/7.35.0 Host: localhost:10000 Accept: */*
CamelNettyRemoteAddress: /127.0.0.1:35302
User-Agent: curl/7.35.0
Content-Length: 2
Connection: keep-alive

ok