修改SOAP信封以包含命名空间

时间:2015-12-30 11:24:21

标签: java soap jaxb cxf jax-ws

我有类似这样的SOAP响应

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
    xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
    <soapenv:Body>
        <calculate_response>
            <tid>33433</tid>
            <status>0</status>
            <reason>0</reason>
        </calculate_response>
    </soapenv:Body>
</soapenv:Envelope>

我需要为它添加名称空间,它应该看起来像这样

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    xmlns:def="http://com.sample.xyz/Messages">
    <soapenv:Body>
        <def:calculate_response>
            <tid>33433</tid>
            <status>0</status>
            <reason>0</reason>
        </def:calculate_response>
    </soapenv:Body>
</soapenv:Envelope>

我使用wsimport创建了存根,我需要在进一步处理之前修改此响应。

实际上,按照我的理解,这是一个两步过程,第一步是将命名空间添加到soap envelop,然后为其添加前缀。 关于前缀部分我找到了this。我不清楚如何做第一步。

注意:我需要在客户端修改响应,我不能在服务器端更改SOAP。

1 个答案:

答案 0 :(得分:0)

我在SOAP Message Handlers

的帮助下实现了这一目标

我写了handler来实现SOAPHandler<SOAPMessageContext> overriddenhandleMessage(SOAPMessageContext context){}以及后续实施。

//Get soapElement from your soap request.
if (soapElement.getTagName().equalsIgnoreCase("calculate_response")) {
       soapElement.setElementQName(
         new QName("http://com.sample.xyz/Messages",
                  "calculate_response",
                  "def"));