Como trocar o prefixo no SOAP

4 respostas
J

Pessoal,

Alguém sabe me dizer porque não consigo trocar esse prefixo? De soapenv para soap
String prefix = "soap";
		String serverURI = "http://tempuri.org/";

		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage soapMessage = messageFactory.createMessage();
		SOAPPart soapPart = soapMessage.getSOAPPart();
		SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
		SOAPBody soapBody = soapEnvelope.getBody();

		String prefixoRemover = soapEnvelope.getPrefix();

		boolean removeuPrefixo = soapEnvelope.removeNamespaceDeclaration(prefixoRemover);
		if (removeuPrefixo) {
			System.out.println("removeu o prefixo do Envelope"); // Passa por aqui com sucesso
		}
		soapEnvelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");

		soapEnvelope.setPrefix(prefix); // Exception aqui

Exception:
[color=red]org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
at org.apache.axiom.om.impl.dom.NodeImpl.setPrefix(NodeImpl.java:127)
at br.com.ps.transp.util.ciot.Teste.createSOAPRequest(Teste.java:78)
at br.com.ps.transp.util.ciot.Teste.main(Teste.java:48)

[/color]

4 Respostas

L
javer:
Pessoal, Alguém sabe me dizer porque não consigo trocar esse prefixo? De soapenv para soap
String prefix = "soap";
		String serverURI = "http://tempuri.org/";

		MessageFactory messageFactory = MessageFactory.newInstance();
		SOAPMessage soapMessage = messageFactory.createMessage();
		SOAPPart soapPart = soapMessage.getSOAPPart();
		SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
		SOAPBody soapBody = soapEnvelope.getBody();

		String prefixoRemover = soapEnvelope.getPrefix();

		boolean removeuPrefixo = soapEnvelope.removeNamespaceDeclaration(prefixoRemover);
		if (removeuPrefixo) {
			System.out.println("removeu o prefixo do Envelope"); // Passa por aqui com sucesso
		}
		soapEnvelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");

		soapEnvelope.setPrefix(prefix); // Exception aqui

Exception:
[color=red]org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
at org.apache.axiom.om.impl.dom.NodeImpl.setPrefix(NodeImpl.java:127)
at br.com.ps.transp.util.ciot.Teste.createSOAPRequest(Teste.java:78)
at br.com.ps.transp.util.ciot.Teste.main(Teste.java:48)

[/color]

A api diz..

NAMESPACE_ERR: Raised if the specified prefix is malformed per the Namespaces in XML specification, if the namespaceURI of this node is null, if the specified prefix is "xml" and the namespaceURI of this node is different from "http://www.w3.org/XML/1998/namespace", if this node is an attribute and the specified prefix is "xmlns" and the namespaceURI of this node is different from " http://www.w3.org/2000/xmlns/", or if this node is an attribute and the qualifiedName of this node is "xmlns" .

Por que quer tornar seu webservice incompativel com a especificação XML?

J
Porque o webservice (que não é meu) espera algo assim:
<?xml version="1.0" encoding="UTF-8"?>

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">

  <soap:Header/>

  <soap:Body>

     <tem:Send>

        <!--Optional:-->

        <tem:message><![CDATA[<?xml version="1.0" encoding="utf-16"?>

<CrossTalk_Message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.nddigital.com.br/nddcargo">

     <CrossTalk_Header>

              <ProcessCode>1000</ProcessCode>

              <MessageType>100</MessageType>

              <ExchangePattern>7</ExchangePattern>

              <GUID>4b41c5bd-82fa-4e4c-9f86-a3c5bff3a927</GUID>

             <DateTime>2012-05-28T11:18:04.2162347-03:00</DateTime>

             <EnterpriseId>12345678901234</EnterpriseId>

             <Token>123456789014257896452135</Token>

     </CrossTalk_Header>

    <CrossTalk_Body>

              <CrossTalk_Version_Body versao=?4.2.0.0?/>

    </CrossTalk_Body>

   </CrossTalk_Message>]]></tem:message>

        <!--Optional:-->

        <tem:rawData><![CDATA[<?xml version="1.0" encoding="utf-8"?>

<loteOT_envio xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" versao="4.1.3.1" token="123456789014257896452135" xmlns="http://www.nddigital.com.br/nddcargo">

 <operacoes>

         <OT>

                 <infOT impAuto="1" Id="4b41c5bd-82fa-4e4c-9f86-a3c5bff3a927" gerPgtoFin="1" gerPgtoPedagio="2">

                         <ide>

                                 <cnpj>12345678901234</cnpj>

                                 <numero>1</numero>

                                 <serie>01</serie>

                                 <ptEmissor>teste</ptEmissor>

                                 <dtInicio>2012-10-29</dtInicio>

                         </ide>

                         <carga>

                                 <padrao>

                                         <codigoSH>0712</codigoSH>

                                         <quantidade>125.00</quantidade>

                                         <remetente>

                                                 <cnpj>11122233000112</cnpj>

                                                 <nome>Razao Social Remetente Exemplo Ltda</nome>

                                                 <endereco>

                                                         <UF>GO</UF>

                                                         <codigoMunicipio>1231234</codigoMunicipio>

                                                         <bairro>exemplo1</bairro>

                                                         <logradouro>Logradouro Exemplo1</logradouro>

                                                         <numero>80</numero>

                                                         <CEP>75460000</CEP>

                                                         <complemento>Complemento de endereço</complemento>

                                                 </endereco>

                                         </remetente>

                                         <destinatario>

                                                 <cnpj>44455566000178</cnpj>

                                                 <nome>Razao Social Destinatario Exemplo Ltda.</nome>

                                                 <endereco>

                                                         <UF>SP</UF>

                                                         <codigoMunicipio>1234567</codigoMunicipio>

                                                         <bairro>Bairro Exemplo2</bairro>

                                                         <logradouro>Logradouro Exemplo2</logradouro>

                                                         <numero>531</numero>

                                                         <CEP>88500000</CEP>

                                                         <complemento>Complemento de endereço</complemento>

                                                 </endereco>

                                         </destinatario>

                                 </padrao>

                                 <proprietarioCarga>1</proprietarioCarga>

                         </carga>

                         <transp>

                                 <rntrc>1235123</rntrc>

                                 <gestorCartao>1</gestorCartao>

                                 <rota>

                                         <rotaERP>1</rotaERP>

                                 </rota>

                                 <condutores>

                                         <condutor>

                                                 <cpf>[telefone removido]</cpf>

                                         </condutor>

                                 </condutores>

                                 <veiculos>

                                         <veiculo>

                                                 <placa>NDD0123</placa>

                                         </veiculo>

                                 </veiculos>

                                 <valores>

                                         <vlrFrete>50.00</vlrFrete>

                                         <parcelamento>

                                                 <regraERP>teste</regraERP>

                                         </parcelamento>

                                         <retencoes>

                                                 <irrf>1.00</irrf>

                                                 <inss>1.00</inss>

                                                 <sestsenat>0.75</sestsenat>

                                         </retencoes>

                                         <tipoRateio>4</tipoRateio>

                                 </valores>

                                 <categoriaPedagio>0</categoriaPedagio>

                         </transp>

                 </infOT>

                 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">

                         <SignedInfo>

                                 <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>

                                 <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>

                                 <Reference URI="4b41c5bd-82fa-4e4c-9f86-a3c5bff3a927">

                                         <Transforms>

                                                 <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>

                                                 <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>

                                         </Transforms>

                                         <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>

                                         <DigestValue>SDFTpEm6rMp6Bub5vrOnc12f1cE=</DigestValue>

                                 </Reference>

                         </SignedInfo>                                

<SignatureValue>...</SignatureValue>

                         <KeyInfo>

                                 <X509Data>                                                <X509Certificate>...</X509Certificate>

                                 </X509Data>

                         </KeyInfo>

                 </Signature>

         </OT>

 </operacoes>

</loteOT_envio>

]]></tem:rawData>

     </tem:Send>

  </soap:Body>

</soap:Envelope>
L

Acho que você vai ter que fazer essa pequena modificação por fora, mas isso em último caso, se API não permitir.

J

Já resolvi, fiz via string e usei um stream para gerar a SoapMessage.

Criado 2 de fevereiro de 2015
Ultima resposta 3 de fev. de 2015
Respostas 4
Participantes 2