使用dataFormat作为POJO通过Camel调用Web Service时无法设置SOAP Header [英] Unable to set SOAP Header while calling Web Service through Camel using dataFormat as POJO
问题描述
我在我们的项目中使用Camel并请求WebServices,其dataFormat为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>
如果只有Body,我将能够发送消息,但是有人可以给我包含头部分的代码段吗? dataFormat是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.
推荐答案
使用dataFormat作为POJO的CXF端点时,Camel Exchange对象中的body是org.apache.cxf.message.MessageContentsList
的对象.它是java.util.ArrayList<Object>
的扩展,按WSDL中定义的顺序包含SOAP消息的各个部分,并包含WebService类中的相应方法.
元素0有一个Body.
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);
}
(未经测试的示例.这只是一个想法,如何处理...)
有关类似问题的其他答案,您可以在这里找到:在骆驼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端点中的拦截器"设置为:
and set Interceptor in Camel Cxf Endpoint as :
<cxfEndpoint ...>
<outInterceptors>
<spring:bean class="MyCxfInterceptor"/>
</outInterceptors>
...
这篇关于使用dataFormat作为POJO通过Camel调用Web Service时无法设置SOAP Header的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!