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

时间:2013-07-10 17:33:11

标签: java xml web-services soap saaj

这两条肥皂信息有效吗?不同的部分是命名空间属性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>

如果第二个肥皂无效,我怎么能和第一个肥皂一样? 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);

2 个答案:

答案 0 :(得分:9)

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

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

上述代码将xmlns="http://www.sigvalue.com/acc"应用于GetNGPList

您的代码,改编:

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

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

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

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

<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>
...

答案 1 :(得分:0)

您可以删除此行:

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

并添加此行

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

一些注释:

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

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