java 在有效的 SOAP SAAJ 信封中使用默认命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17577207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:22:05  来源:igfitidea点击:

Use default namespace in valid SOAP SAAJ envelope

javaxmlweb-servicessoapsaaj

提问by Frank

Are these two soap messages valid ? The different part is the namespace attribute xmlns="http://www.sigvalue.com/acc". The first soap is a sample, the second one generated by java code to make the same soap message.

这两条soap消息有效吗?不同的部分是命名空间属性 xmlns="http://www.sigvalue.com/acc"。第一个soap是一个示例,第二个由java代码生成以生成相同的soap消息。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
  <GetNGPList xmlns="http://www.sigvalue.com/acc">
  <UserData>
    <senderId>string</senderId>
    <channelId>string</channelId>
    <timeStamp>dateTime</timeStamp>
  </UserData>
  <zipCode>string</zipCode>
</GetNGPList>
 </soap:Body>
</soap:Envelope>

.

.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetNGPList>
  <UserData xmlns="http://www.sigvalue.com/acc">
    <senderId>string</senderId>
    <channelId>string</channelId>
    <timeStamp>dateTime</timeStamp>
  </UserData xmlns="http://www.sigvalue.com/acc">
  <zipCode>string</zipCode>
</GetNGPList>
</soap:Body>
</soap:Envelope>

If the second soap is invalid, how could I make it the same as the first one? GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc"); this line doesn't work as expected. Here is the JAVA code :

如果第二个肥皂无效,我如何使它与第一个相同?GetNGPList.addNamespaceDeclaration("xmlns"," http://www.sigvalue.com/acc"); 这条线没有按预期工作。这是JAVA代码:

    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();

    SOAPPart soapPart = soapMessage.getSOAPPart();


    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("xsi", gXSIServerURI);
    envelope.addNamespaceDeclaration("xsd", gXSDServerURI);

        // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement GetNGPList =  soapBody.addChildElement("GetNGPList");
    GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc");

    SOAPElement UserData = GetNGPList.addChildElement("UserData");
    SOAPElement senderId = UserData.addChildElement("senderId");
    SOAPElement channelId = UserData.addChildElement("channelId");
    SOAPElement timeStamp = UserData.addChildElement("timeStamp");
    senderId.addTextNode("string");
    channelId.addTextNode("string");
    timeStamp.addTextNode("dateTime");

    SOAPElement zipCode = GetNGPList.addChildElement("zipCode");
    zipCode.addTextNode("string"); 


    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction",  "sample");

    soapMessage.saveChanges();

    /* Print the request message */
    //System.out.print("Request SOAP Message = ");
    soapMessage.writeTo(System.out);

回答by acdcjunior

To set a namespace as the default namespace, simply use the empty string ("") as prefix name:

要将命名空间设置为默认命名空间,只需使用空字符串 ( "") 作为前缀名称

SOAPElement GetNGPList =
       soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The code above will apply xmlns="http://www.sigvalue.com/acc"to GetNGPList.

上面的代码将适用xmlns="http://www.sigvalue.com/acc"GetNGPList.

Your code, adapted:

你的代码,改编:

// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement GetNGPList =
       soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");

SOAPElement UserData = GetNGPList.addChildElement("UserData");
...

As usual, when you omit the namespace prefix declaration in addChildElement(), the child inherits its namespace from its parent.

像往常一样,当您省略 中的命名空间前缀声明时addChildElement(),子级将从其父级继承其命名空间。

The code above will generate what you needed:

上面的代码将生成您需要的内容:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <GetNGPList xmlns="http://www.sigvalue.com/acc">
            <UserData>
                <senderId>string</senderId>
...

回答by Vegard

You can remove this line:

您可以删除此行:

GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc");

and add this line

并添加这一行

envelope.addNamespaceDeclaration("","http://www.sigvalue.com/acc");

a few notes:

一些注意事项:

  • I suggest using frameworks for working with web services, Apache CXF can do a lot of the tricky work for you: http://cxf.apache.org/

  • make sure to follow normal naming standards, it makes in easier to read the code. The variable GetNGPListshould be getNGPList

  • 我建议使用框架来处理 Web 服务,Apache CXF 可以为您完成很多棘手的工作:http: //cxf.apache.org/

  • 确保遵循正常的命名标准,这样更容易阅读代码。变量GetNGPList应该是getNGPList