【问题标题】:Modify Soap header in SoapHandler在 SoapHandler 中修改 Soap 标头
【发布时间】:2015-03-04 11:21:02
【问题描述】:

我正在尝试修改一个肥皂标题,我希望标题是这样的

<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: 尝试创建 或以不正确的方式更改对象 命名空间。

首先是 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;

【问题讨论】:

  • 是否有可能必须先删除旧的标头属性才能添加新的标头属性?看看这篇 SO 文章:stackoverflow.com/questions/7473145/…
  • 默认没有属性。
  • 尝试添加每个属性,一次一个,看看是否可以在标题上设置任何属性。
  • 另外请向我们展示您的 SOAP 导入语句以及您使用的 JAR。

标签: java web-services soap soapheader


【解决方案1】:

您收到此错误是因为您试图在 SOAP 标头中定义名称空间属性。命名空间属性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.addAttribute(),但我什至找不到。

【讨论】:

    【解决方案2】:

    终于有点运气了。

    此代码有效

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

    添加标题后不要忘记保存消息

    context.getMessage().saveChanges();
    

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

    如果完成任何更改,也会保存消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-16
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      相关资源
      最近更新 更多