CXF中将自定义Soap

CXF中将自定义Soap

本文介绍了在Camel-CXF中将自定义Soap-Header设置为pojo消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对cxf肥皂标题有问题.我用Contract-firs-development方法建立了一个cxf项目.我想用cxf-component调用一个Web服务,它看起来像这样.

I have a Problem with cxf soap-headers. I set up an cxf-project with the Contract-firs-development method. I want to call a web-service with an cxf-component, which looks like this.

<cxf:cxfEndpoint id="ICCSCustomerService"
                 address="http://localhost:8080/iccs-xsoap/CustomerService/updateCustomer"
                 serviceClass="de.iccs.xsoap.customer.v1.CustomerServiceImpl" >
</cxf:cxfEndpoint>

我想发送pojo消息,将直接组件作为对ws的请求.我的路线如下:

I want to send an pojo-message throw an direct-component as an request for the ws. My route looks like this:

<route id="CustomerServiceUpdateCustomerTest">
        <camel:from uri="direct:iccsUpdateCustomerRequest"/>
        <camel:process ref="addCredentials"/>
        <to uri="cxf:bean:ICCSCustomerService"/>
        <camel:to uri="stream:out"/>
</route>

我需要实现一个类似这样的soap标头:

I need to implement a soap header like this one:

<ns2:Header>
    <simpleAuth xmlns="http://xsoap.iccs.de/v1" password="abc" username="xxx"/>
</ns2:Header>

要对此进行存档,我编写了一个类似的处理器(另请参见 http://camel.apache上的示例.org/cxf.html ):

To archive this I wrote an processor like this one (see also example at http://camel.apache.org/cxf.html):

@Override
public void process(Exchange exchange) throws Exception {
   List<SoapHeader> soapHeaders = CastUtils.cast((List<?)exchange.getOut().getHeader(Header.HEADER_LIST));
    // Insert a new header
    String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><outofbandHeader "
        + "xmlns=\"http://cxf.apache.org/outofband/Header\" hdrAttribute=\"testHdrAttribute\" "
        + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:mustUnderstand=\"1\">"
        + "<name>simpleAuth username=\"xxx\" password=\"abc\" xmlns=\"http://xsoap.iccs.de/v1\"</name></outofbandHeader>";

    SoapHeader newHeader = new SoapHeader(new QName("http://xsoap.iccs.de/v1", "simpleAuth"),
                   DOMUtils.readXml(new StringReader(xml)).getDocumentElement());
    // make sure direction is OUT since it is a response message.
    newHeader.setDirection(Direction.DIRECTION_OUT);
    //newHeader.setMustUnderstand(false);
    soapHeaders.add(newHeader);
}

不幸的是,我在此语句中得到了一个空指针异常: 列出soapHeaders = CastUtils.cast((清单

显然,此消息中没有任何肥皂标头.它似乎根本不是肥皂信息.像这样编组

Unfortunately I get an null-pointer Exception in this statement: List soapHeaders = CastUtils.cast((List

Obviously there are no soap-headers in this message. And it seems not to be an soap message at all.Marschaling like this

       <camel:marshal>
            <soapjaxb contextPath="de.iccs.xsoap.customer.v1" />
        </camel:marshal>
        <camel:process ref="addCredentials"/>

不起作用,因为它仅产生一个肥皂信封,而没有肥皂标题. (当然,这不起作用,因为cxf-endpoint在pogo模式下工作)您能否提供一个示例,说明如何从pojo消息中设置一条肥皂消息(带有soapheader).

does not work, as it produces only an soap-envelope without an soap-header. (and of corse this dosen't work as cxf-endpoint works in pogo-mode)Could you provide me an example how to set an soap message (with soap-header) from an pojo-message.

谢谢加布里埃尔

推荐答案

不知道您是否已经解决了问题,但我也遇到过类似的情况,因此也许有人会受益.

Don't know if you solved your problem already, but I experienced something similar as well, so maybe someone will benefit.

如果您的NPE是由于没有现有的标头,则在需要时创建一个新列表是完全可以接受的.

If your NPE is due to there being no existing headers, it is perfectly acceptable to create a new list if needed.

if (message.getHeader(Header.HEADER_LIST) == null) {
    message.setHeader(Header.HEADER_LIST, new ArrayList<SoapHeader>());
}

但是使用手工制作的XML填充SoapHeader可能会遇到另一个问题.您仍在使用原始CXF示例中的outofbandHeader元素;这是示例中的特定标头,不是带外标头的常规包装.另外,您的simpleAuth未被标记为元素(尽管很难阅读...).

But you may have another problem with the hand-crafted XML being used to populate the SoapHeader. You're still using the outofbandHeader element from the original CXF example; this is a specific header in the example, not a general wrapper for out-of-band headers. Also, your simpleAuth is not marked up as an element (although it's difficult to read...).

如果已为您的simpleAuth元素添加了带有@XMLRootElement的注释类(生成或创建),则可以使用采用JAXBDataBinding的SoapHeader构造函数.CXF将为您封送标题.

If you have annotated class (generated or created) with an @XMLRootElement for your simpleAuth element, you can use the SoapHeader constructor that takes a JAXBDataBinding.CXF will marshal the header for you.

@Override
public void process(Exchange exchange) throws Exception {

    Message in = exchange.getIn();
    if (in.getHeader(Header.HEADER_LIST) == null) {
        in.setHeader(Header.HEADER_LIST, new ArrayList<SoapHeader>());
    }
    List<SoapHeader> headers = CastUtils.cast((List<?>)in.getHeader(Header.HEADER_LIST));

    SimpleAuth auth = new SimpleAuth();
    auth.setUsername("xxx");
    auth.setPassword("abc");

    try {
        SoapHeader header = new SoapHeader(new QName("http://xsoap.iccs.de/v1", "simpleAuth"),
                auth, new JAXBDataBinding(SimpleAuth.class));
        header.setDirection(Direction.DIRECTION_OUT);
        header.setMustUnderstand(true);
        soapHeaders.add(header);
    } catch (JAXBException e) {
    e.printStackTrace();
    }
}

这篇关于在Camel-CXF中将自定义Soap-Header设置为pojo消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:04