修改 SoapHandler 中的 Soap header

Modify Soap header in SoapHandler

我正在尝试修改肥皂header,我希望header像这样

<soapenv:Header xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ns:authnHeader soapenv:mustUnderstand="0" xmlns:ns="http://webservices.averittexpress.com/authn">
                <Username>xxxxxxxx</Username>
                <Password>xxxxxxxx</Password>
            </ns:authnHeader>

这是我到目前为止所做的...

SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();

    header.addAttribute(new QName("xmlns:soapenc"), "http://schemas.xmlsoap.org/soap/encoding/");
    header.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
    header.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");

SOAPElement authnHeader = header.addChildElement("authnHeader", "ns" , "http://webservices.averittexpress.com/authn");

authnHeader.addAttribute(new QName("soapenv:mustUnderstand"), "0");

但我得到

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

起初header.addAttribute.

请帮忙。

我的进口报表

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.PortInfo;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

您收到此错误是因为您试图在 SOAP header 中定义名称空间属性。名称空间属性 xmlns 必须 在 SOAP 信封中定义。所以你真正想要的 XML SOAP 信封看起来像这样:

<soap:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Header>
        <ns:authnHeader soapenv:mustUnderstand="0" xmlns:ns="http://webservices.averittexpress.com/authn">
            <Username>xxxxxxxx</Username>
            <Password>xxxxxxxx</Password>
        </ns:authnHeader>
    </soap:Header>
    <soap:Body>
        <!-- your content goes here -->
    </soap:Body>
</soap:Envelope>

根据 conventions,如果您的 XML 没有在信封中提供 SOAP 命名空间,应用程序可能会拒绝您的 SOAP 消息。

作为参考,我花了大约 3 个小时试图找到一个代码示例,其中有人在 SOAP header 上调用 header.addAttribute(),但我什至找不到一个。

终于有点运气了。

此代码有效

Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

if(outboundProperty.booleanValue())
{

   try
    {
         SOAPHeader header = context.getMessage().getSOAPPart().getEnvelope().getHeader();

         SOAPFactory soapFactory = SOAPFactory.newInstance();
         SOAPElement authnHeader = soapFactory.createElement("authnHeader", "ns", "http://webservices.averittexpress.com/authn");

         SOAPElement username = authnHeader.addChildElement("Username");
         username.setTextContent("xxxxxxx");

         SOAPElement password = authnHeader.addChildElement("Password");
         password.setTextContent("xxxxxxx");

         header.addChildElement(authnHeader);
    }
    catch(Exception e)
    {
       e.printStackTrace();
    }
}

添加后header不要忘记保存留言

context.getMessage().saveChanges();

context.getMessage().writeTo(System.out);

如果进行了任何更改,也会保存消息。