java - 如何从 Camel 中的交换对象中检索 SOAP 标头?
问题描述
- 我们公开了一个 Apache camel-cxf 网络服务。使用骆驼处理器,我们试图从soapUi客户端获取我们在soap请求中传递的soap标头。
交换对象包含肥皂消息的主体(不是肥皂头)。在 exchange.getIn.getHeader() 中,我们只获取我们传递的 HTTP 标头,而不是 SOAP 标头。
如何从 Camel 中的交换对象中检索 SOAP 标头?
以下是通过的soap标头:
<soapenv:Header>
<ns1:info xmlns:ns1="http://www.w3schools.com/transaction/">
<ns1:TransactionID>01</ns1:TransactionID>
<ns1:AppUserID>52</ns1:AppUserID>
<ns1:AppPass>ab</ns1:AppPass>
</ns1:info>
</soapenv:Header>
我们尝试使用,
- 交换.getIn().getHeaders();
exchange.getIn().getHeader(“TransactionID”);
但是,它无法提取发送的 SOAP 标头。
- 如何在有效负载模式下添加自定义肥皂标题?
解决方案
如果 cxf 端点配置为在 DataFormat.PAYLOAD 中工作,则:
.process(exchange -> {
CxfPayload body = exchange.getIn().getBody(CxfPayload.class);
for (Object header : body.getHeaders()) {
SoapHeader soapHeader = (SoapHeader) header;
org.w3c.dom.Element element = (Element) soapHeader.getObject();
//parse elements
}
});
如果在原始模式下,只需使用 xpath 读取输入流并将数据解析为 xml
升级版:
示例路线:
CxfEndpoint endpoint = new CxfEndpoint();
endpoint.setDataFormat(DataFormat.PAYLOAD);
endpoint.setWsdlURL("etc/Proxy.wsdl");
endpoint.setAddress("http://localhost:8089/wsservice/");
getContext().getRegistry().bind("cxfend", endpoint);
from("cxf:bean:cxfend")
.process(exchange -> {
CxfPayload body = exchange.getIn().getBody(CxfPayload.class);
for (Object header : body.getHeaders()) {
SoapHeader soapHeader = (SoapHeader) header;
org.w3c.dom.Element element = (Element) soapHeader.getObject();
Element transactionID = (Element) element.getElementsByTagName("ns1:TransactionID").item(0);
log.info("Header TransactionID with value:{}", transactionID.getTextContent());
}
});
输出:
2020-03-03 11:43:03,327 [main ] INFO Server
- jetty-9.4.21.v20190926; built: 2019-09-26T16:41:09.154Z; git:
72970db61a2904371e1218a95a3bef5d79788c33; jvm 1.8.0_232-b18
2020-03-03 11:43:03,377 [main ] INFO AbstractConnector
- Started ServerConnector@b016b4e{HTTP/1.1,[http/1.1]}{localhost:8089}
2020-03-03 11:43:03,377 [main ] INFO Server
- Started @3403ms
2020-03-03 11:43:03,400 [main ] INFO ContextHandler
- Started o.e.j.s.h.ContextHandler@1b15f922{/wsservice,null,AVAILABLE}
2020-03-03 11:43:03,400 [main ] INFO DefaultCamelContext
- Route: route1 started and consuming from: cxf://bean:cxfend
2020-03-03 11:43:03,406 [main ] INFO DefaultCamelContext
- Total 1 routes, of which 1 are started
2020-03-03 11:43:03,408 [main ] INFO DefaultCamelContext
- Apache Camel 3.0.1 (CamelContext: camel-1) started in 1.953 seconds
2020-03-03 11:43:10,068 [tp1299661385-22] INFO TypeUtil
- JVM Runtime does not support Modules
2020-03-03 11:43:10,196 [tp1299661385-22] INFO RouteTest
- Header TransactionID with value:01
推荐阅读
- android - 如何检测密钥是否在 TEE 或钛芯片内部生成
- rust - 内置测试器“定义”没有像我预期的那样工作
- haskell - 如何建立对 Servant 类型错误的直觉?
- oracle - 如何使用 Oracle XML DB Webservices 将表作为一行返回
- android - 从服务器获取数据后如何缓存数据?
- c# - 将文件分类到目录中
- r - Shiny 中的 Kmeans 实现
- wordpress - 如何使用 elementor 在 wordpress 中从头开始自定义页眉和页脚?
- android - “getDisplayLanguage()”总是返回“English”
- firebase-dynamic-links - 如何让我的 Firebase 动态链接重定向到我在桌面上的网站和我在移动设备上的即时应用