使用 dataFormat 作为 POJO 通过 Camel 调用 Web 服务时无法设置 SOAP 标头 [英] Unable to set SOAP Header while calling Web Service through Camel using dataFormat as POJO
问题描述
我在我们的项目中使用 Camel 并请求 WebServices,数据格式为 POJO.当我的 SOAP 消息不包含 SOAP 标头时,我能够请求,但是当它有标头时,我无法设置它们.我查看了文档,但无法理解并有几个问题.
I am using Camel in our project and requesting WebServices, the dataFormat is POJO. I was able to request when my SOAP message did not contain SOAP headers, but when it had Headers, I was unable to set those. I looked at the documentation but was not able to understand and have several questions.
我想创建如下消息:
<soapenv:Envelope`enter code here`
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<platformMsgs:documentInfo
xmlns:platformMsgs="urn:messages_2015_1.platform.webservices.netsuite.com">
<platformMsgs:nsId>WEBSERVICES_3479023</platformMsgs:nsId>
</platformMsgs:documentInfo>
</soapenv:Header>
<soapenv:Body>
<addListResponse
xmlns="">
<platformMsgs:writeResponseList
xmlns:platformMsgs="urn:messages_2015_1.platform.webservices.netsuite.com">
<platformCore:status isSuccess="true"
xmlns:platformCore="urn:core_2015_1.platform.webservices.netsuite.com"/>
<platformMsgs:writeResponse>
<platformCore:status isSuccess="false"
xmlns:platformCore="urn:core_2015_1.platform.webservices.netsuite.com">
<platformCore:statusDetail type="ERROR">
<platformCore:code>DUP_ENTITY</platformCore:code>
<platformCore:message>This entity already exists.</platformCore:message>
</platformCore:statusDetail>
</platformCore:status>
</platformMsgs:writeResponse>
</platformMsgs:writeResponseList>
</addListResponse>`enter code here`
</soapenv:Body>
</soapenv:Envelope>
如果只有正文,我将能够发送消息,但是有人可以给我一个包含标题部分的代码片段吗?数据格式为 POJO.
I will be able to send the message if there was only Body, but can someone give me a code snippet for including the header section? The dataFormat is POJO.
推荐答案
当使用 CXF 端点和 dataFormat 作为 POJO 时,Camel Exchange 对象中的 body 是 org.apache.cxf.message.MessageContentsList
.它是 java.util.ArrayList
的扩展,它包含 SOAP Message 的部分,按照 WSDL 中定义的顺序以及 WebService 类中的相应方法.元素 0 有一个正文.
When using CXF endpoint with dataFormat as POJO, body in Camel Exchange object is an object of org.apache.cxf.message.MessageContentsList
. It is an extension of java.util.ArrayList<Object>
and it contains parts of SOAP Message in order as defined in WSDL and corresponding method in WebService class.
Element 0 there is a Body.
因此,使用 Java 执行此操作的一种方法是创建一个实现 org.apache.camel.Processor
接口的 Processor 类,并在其 process
方法中设置您的 SOAP 标头.类似的东西:
So, one way to do that with Java is to create a Processor class implementing org.apache.camel.Processor
interface and in its process
method set your SOAP header. Something like:
@Override
public void process(Exchange camelExchange) throws Exception {
MessageContentsList messageBody = (MessageContentsList) camelExchange.getIn().getBody();
DocumentInfo docInfoHeader = new DocumentInfo();
... set docInfoHeader properties ...
messageBody.add(docInfoHeader);
}
(样本未经测试.这只是一个想法,如何处理……)
关于类似问题的其他答案,您可以在这里找到:在 Camel Cxf 中设置自定义 Soap Header-To Pojo 消息
Other answer on similar question you can find here: Setting Custom Soap Header-To Pojo Message In Camel Cxf
它描述了如何将 Camel Exchange 标头用作 SOAP 标头.
It describes how to use Camel Exchange headers as SOAP Headers.
我不确定 100% 哪种方式适合您,哪种方式更好...我想,这取决于您使用的 WSDL.
I'm not sure for 100% which way will work for you and which one is better... I guess, it depends on WSDL you use.
UPD:第二种选择是通过使用 CxfMessageSoapHeaderOutInterceptor
自定义实现来使用纯 CXF 解决方案.它可能看起来像:
UPD: second choice is to use pure CXF solution by using CxfMessageSoapHeaderOutInterceptor
custom implementation.
It may look like:
public class MyCxfInterceptor extends CxfMessageSoapHeaderOutInterceptor {
@Override
public void handleMessage( org.apache.cxf.binding.soap.SoapMessage message) {
org.apache.cxf.binding.soap.SoapHeader myCustomHeader = new org.apache.cxf.binding.soap.SoapHeader(new QName(
{custom name space}, {custom local name}), {Custom content object}));
myCustomHeader.setMustUnderstand(true);
message.getHeaders().add(myCustomHeader);
}
并将 Camel Cxf Endpoint 中的 Interceptor 设置为:
and set Interceptor in Camel Cxf Endpoint as :
<cxfEndpoint ...>
<outInterceptors>
<spring:bean class="MyCxfInterceptor"/>
</outInterceptors>
...
这篇关于使用 dataFormat 作为 POJO 通过 Camel 调用 Web 服务时无法设置 SOAP 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!