在 Camel-CXF 中将自定义 Soap-Header 设置为 pojo-message [英] Setting custom Soap-Header to pojo-message in Camel-CXF

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

问题描述

我对 cxf 肥皂头有问题.我使用合同优先开发方法设置了一个 cxf 项目.我想用一个 cxf 组件调用一个 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-message 抛出一个直接组件作为对 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); 
}

不幸的是,我在这条语句中得到了一个空指针异常:List soapHeaders = CastUtils.cast((List

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

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"/>

不起作用,因为它只产生一个没有肥皂头的肥皂信封.(和 corse 这不工作,因为 cxf-endpoint 在 pogo 模式下工作)您能否为我提供一个如何从 pojo 消息设置肥皂消息(带有肥皂头)的示例.

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>());
}

但是您可能对用于填充 SoapHeader 的手工制作的 XML 有另一个问题.您仍在使用原始 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-message的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆