Pergunta

Eu preciso escrever um cliente SOAP para enviar uma mensagem de pedido, eu poderia enviar com êxito a solicitação, mas eu preciso de alterar a mensagem, a única modificação é adicionar o prefixo para os elementos filho.Uma vez eu usei o seguinte código, mas nada acontece.

WebsiteConfigID.addNamespaceDeclaration("v3", "http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");

Corrente de saída

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" 
              xmlns:v3="http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2">
   <env:Header/>
   <env:Body>
       <GetEvents>
          <websiteConfigID>1111</websiteConfigID>
          <cityZip>Paris</cityZip>
       </GetEvents>
   </env:Body>
</env:Envelope>

Saída esperada

 <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" 
              xmlns:v3="http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2">
   <env:Header/>
   <env:Body>
       <v3:GetEvents>     <<prefix is added
          <v3:websiteConfigID>1111</v3:websiteConfigID>  <<prefix is added
          <v3:cityZip>Paris</v3:cityZip>  <<prefix is added
       </v3:GetEvents>  
   </env:Body>
 </env:Envelope>

Código

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

    SOAPConnection connection = soapConnectionFactory.createConnection();

    SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();



    SOAPBody body = message.getSOAPBody();
    SOAPPart part = message.getSOAPPart();
    SOAPEnvelope envelope = part.getEnvelope();
    envelope.addNamespaceDeclaration("v3", "http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");
    SOAPFactory soapFactory = SOAPFactory.newInstance();

    Name bodyName;
    bodyName = soapFactory.createName("GetEvents");

    SOAPBodyElement getEvents = body.addBodyElement(bodyName);
    Name childName = soapFactory.createName("websiteConfigID");
    SOAPElement WebsiteConfigID = getEvents.addChildElement(childName);
    WebsiteConfigID.addTextNode("1111");

    childName = soapFactory.createName("cityZip");
    SOAPElement CityZip = getEvents.addChildElement(childName);
    CityZip.addTextNode("Paris");

    message.writeTo(System.out);
Foi útil?

Solução

Use o SOAPFactory métodos que levam QName, ou prefixo e informações de uri.Por exemplo, em vez de chamar bodyName = soapFactory.createName("GetEvents");, ele deve ser

bodyName = soapFactory.createName("GetEvents", "v3",
   "http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");

Consulte aqui para obter mais detalhes sobre createName método

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top