有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在使用dataFormat作为POJO通过Camel调用Web服务时无法设置SOAP标头

我在我们的项目中使用Camel并请求WebServices,数据格式是POJO。当我的SOAP消息不包含SOAP头时,我可以请求,但当它包含头时,我无法设置这些头。我查看了文档,但无法理解,并且有几个问题

我想创建一条如下所示的消息:

<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


共 (2) 个答案

  1. # 1 楼答案

    假设我请求Web服务,但它失败了,就会生成一条错误消息。我是否也会在MessageContentsList的位置0处获取Fault对象?或者我将只获得位置0处的响应对象

  2. # 2 楼答案

    使用dataFormat为POJO的CXF端点时,Camel Exchange对象中的主体是org.apache.cxf.message.MessageContentsList的对象。它是java.util.ArrayList<Object>的扩展,按照WSDL中定义的顺序和WebService类中相应的方法包含SOAP消息的部分。 元素0有一个主体

    因此,使用Java实现这一点的一种方法是创建一个实现org.apache.camel.Processor接口的处理器类,并在其process方法中设置SOAP头。比如:

    @Override
    public void process(Exchange camelExchange) throws Exception {
    
      MessageContentsList messageBody = (MessageContentsList) camelExchange.getIn().getBody(); 
    
       DocumentInfo docInfoHeader = new DocumentInfo();
       ... set docInfoHeader properties ...
       messageBody.add(docInfoHeader);
    
    }
    

    (样本未经测试。它只是一个想法,如何处理…)

    关于类似问题的其他答案可以在这里找到:Setting Custom Soap Header-To Pojo Message In Camel Cxf

    它描述了如何将Camel交换头用作SOAP头

    我不能100%确定哪种方法对你有效,哪种更好。。。 我想,这取决于您使用的WSDL

    UPD:第二种选择是通过使用CxfMessageSoapHeaderOutInterceptor自定义实现来使用纯CXF解决方案。 它可能看起来像:

    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端点中的拦截器设置为:

    <cxfEndpoint ...>
        <outInterceptors>
            <spring:bean class="MyCxfInterceptor"/>
        </outInterceptors>
    ...