添加不带名称空间的SOAP标头元素 [英] Adding SOAP header elements without namespace

查看:608
本文介绍了添加不带名称空间的SOAP标头元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在SOAP信封中添加标头元素.我的每一次尝试都会引发异常:

I want to add a header element in SOAP envelope. Each of my attempts throws the exception:

com.sun.xml.internal.messaging.saaj.soap.impl.HeaderImpl addHeaderElement严重:SAAJ0131:HeaderElement必须是名称空间限定的

com.sun.xml.internal.messaging.saaj.soap.impl.HeaderImpl addHeaderElement SEVERE: SAAJ0131: HeaderElements must be namespace qualified

我只需要在标题内添加一个不带名称空间的简单元素.在这种情况下,我使用了处理程序:

I need to add just a simple element without namespace inside the header. In this case, I have used a handler:

public boolean handleMessage(SOAPMessageContext context) {
    try {
        SOAPMessage message = context.getMessage();
        SOAPHeader header = message.getSOAPHeader();
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        if (header == null) {
            header = envelope.addHeader();
        }
        envelope.addNamespaceDeclaration("gat", "http://schemas.datacontract.org/2004/07/Gateway.Servicios");

        QName passwordQname = header.createQName("Password", "gat");
        QName userQname = header.createQName("User", "gat");

        // Here, I’m trying to add a QName with no namespace.
        QName qNameUserCredentials = new QName(XMLConstants.NULL_NS_URI, "MyHeader");
        SOAPHeaderElement userCredentials = header.addHeaderElement(qNameUserCredentials);

        SOAPHeaderElement password = header.addHeaderElement(passwordQname);
        password.addTextNode(this.password);

        SOAPHeaderElement username = header.addHeaderElement(userQname);
        username.addTextNode(this.username);

        userCredentials.addChildElement(password);
        userCredentials.addChildElement(username);
        message.saveChanges();

        StringWriter writer = new StringWriter();
        message.writeTo(new StringOutputStream(writer));
        System.out.println("SOAP message: \n" + writer.toString());
    } catch (SOAPException e) {
        System.out.println("Error occurred while adding credentials to SOAP header." + e.getMessage());
    } catch (IOException e) {
        System.out.println("Error occurred while writing message to output stream." + e.getMessage());
    }
    return true;
}

预期输出:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gat="http://schemas.datacontract.org/2004/07/Gateway.Servicios" xmlns:tem="http://tempuri.org/">
    <soapenv:Header>
        <MyHeader>   <-- ######## -->
           <gat:Password>da92767c161e4a62b49f23cb8e712d8e</gat:Password>
            <gat:User>3f3a1850e523fd9961cfa2f4d36bd44d</gat:User>
        </MyHeader>  <-- ######## -->
    </soapenv:Header>
    <soapenv:Body>
        <tem:ConsultarMediosPago>
            <tem:codigoPais>MX</tem:codigoPais>
        </tem:ConsultarMediosPago>
    </soapenv:Body>
</soapenv:Envelope>

元素<MyHeader></MyHeader>是我需要添加的.

推荐答案

这是不可能的,并且错误消息指出它很清楚.

It's impossible and the fault message states it pretty clear.

请参见 http://www.w3schools.com/XML/xml_soap.asp在"SOAP标头元素"部分

See http://www.w3schools.com/XML/xml_soap.asp on section 'The SOAP Header Element'

注意: Header元素的所有直接子元素必须是名称空间限定的

Note: All immediate child elements of the Header element must be namespace-qualified

这篇关于添加不带名称空间的SOAP标头元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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