2013-07-10 15 views
6

Questi due messaggi di sapone sono validi? La parte diversa è l'attributo namespace xmlns = "http://www.sigvalue.com/acc". Il primo soap è un campione, il secondo generato dal codice java per creare lo stesso messaggio soap.Utilizzare lo spazio dei nomi predefinito nella busta valida SAAP SAAJ

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

Se la seconda soap non è valido, come potrei fare lo stesso come il primo? GetNGPList.addNamespaceDeclaration ("xmlns", "http://www.sigvalue.com/acc"); questa linea non funziona come previsto. Ecco il codice 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); 

risposta

8

Per impostare un namespace namespace predefinito, è sufficiente utilizzare il stringa vuota ("") come prefisso nome:

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

il codice sopra vale xmlns="http://www.sigvalue.com/acc" a GetNGPList.

Il codice, adattato:

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

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

Come al solito, quando si omette la dichiarazione di namespace prefix nel addChildElement(), il figlio eredita il suo spazio dei nomi dal suo genitore.

Il codice di cui sopra genererà ciò che è necessario:

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

Questo funziona, grazie! – Frank

+0

Hai fatto la mia giornata! – ForguesR

0

È possibile rimuovere questa riga:

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

e aggiungere questa linea

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

alcune note:

  • I suggerire l'uso di framework per Apache CXF può fare un sacco di lavoro per voi: http://cxf.apache.org/

  • assicurarsi di seguire i normali standard di denominazione, facilita la lettura del codice. La variabile GetNGPList dovrebbe essere getNGPList