Send XML file to webservice SOAP with Java class


Send XML file to webservice SOAP with Java class



I need to send an XML file through a java class to a SOAP webservice.
I'm getting the following error message:



com.sun.xml.ws.fault.ServerSOAPFaultException:
Client received SOAP Fault from server:
Cannot find dispatch method for {}Contribuinte
Please see the server log to find more detail regarding exact cause of the failure.



Here's the XML I want to send


<?xml version="1.0" encoding="ISO-8859-1"?>
<Contribuinte xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='Contribuinte.xsd'>
<Nome>CONTRIBUINTE TESTE INTEGRAÇÃO</Nome>
<NomeFantasia>CONTRIBUINTE TESTE INTEGRAÇÃO</NomeFantasia>
<TipoPessoa>J</TipoPessoa>
<Documento>16619539000163</Documento>
<InscricaoMunicipal>152165</InscricaoMunicipal>
<Rg>2185188</Rg>
<InscricaoEstadual>151616</InscricaoEstadual>
<DataNascimento></DataNascimento>
<NomeMunicipio>DOMINGOS MARTINS</NomeMunicipio>
<SiglaEstado>ES</SiglaEstado>
<NomeBairro>CENTRO</NomeBairro>
<Cep>29260000</Cep>
<NomeTipoLogradouro>RUA</NomeTipoLogradouro>
<NomeLogradouro>NOME DA RUA</NomeLogradouro>
<Numero>0</Numero>
<Complemento>CASA</Complemento>
<Email>email@email.com.br</Email>
<Telefone>2733333333</Telefone>
<Celular>27999999999</Celular>
<OptanteSimplesNacional>N</OptanteSimplesNacional>
<DataInclusaoSimplesNacional></DataInclusaoSimplesNacional>
<SituacaoCadastral>A</SituacaoCadastral>
<ObservacaoCadastro>Observação do cadastro</ObservacaoCadastro>
<CodigoRegimeTributacao>0</CodigoRegimeTributacao>
<DocumentoContador></DocumentoContador>
<NomeContador></NomeContador>
<Atividades>
<Atividade>
<CodigoAtividade>000000004</CodigoAtividade>
<NomeAtividade>Averbação</NomeAtividade>
</Atividade>
<Atividade>
<CodigoAtividade>000000009</CodigoAtividade>
<NomeAtividade>Certidão Detalhada c/ Fração Ideal - Unidade</NomeAtividade>
</Atividade>
</Atividades>
<Servicos>
<Servico>
<CodigoServico>00001</CodigoServico>
</Servico>
<Servico>
<CodigoServico>00020</CodigoServico>
</Servico>
</Servicos>
</Contribuinte>



My method that tries to send information


public static void main(String args) {
Test m = new Test(); // this main method belongs to class Test
String targetNameSpace = "http://nfse.el.com.br";
String endpointUrl = "http://10.1.1.136:8080/el-nfse-integracao/Nfse";
QName serviceName = new QName(targetNameSpace, "Nfse");
QName portName = new QName(targetNameSpace, "NfsePort");
String SOAPAction = "http://nfse.el.com.br/Contribuinte";

SOAPMessage response;

try {
response = m.invoke(serviceName, portName, endpointUrl, SOAPAction);
if (response.getSOAPBody().hasFault()) {
System.out.println(response.getSOAPBody().getFault());
} else {
System.out.println("ok");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

public SOAPMessage invoke(QName serviceName, QName portName, String endpointUrl, String soapActionUri) throws Exception {
/** Create a service and add at least one port to it. **/
Service service = Service.create(serviceName);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);

/** Create a Dispatch instance from a service.**/
Dispatch dispatch = service.createDispatch(portName,
SOAPMessage.class, Service.Mode.MESSAGE);

// The soapActionUri is set here. otherwise we get a error on .net based services.
dispatch.getRequestContext().put(Dispatch.SOAPACTION_USE_PROPERTY, new Boolean(true));
dispatch.getRequestContext().put(Dispatch.SOAPACTION_URI_PROPERTY, soapActionUri);

String guiaForCDATA = "";

guiaForCDATA += "<?xml version="1.0" encoding="UTF-8"?>"+
"<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">"+
" <SOAP-ENV:Body>";

guiaForCDATA += getXml(); //

guiaForCDATA += " </SOAP-ENV:Body> </SOAP-ENV:Envelope>";

/** Create SOAPMessage request. **/
// compose a request message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();

//Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
body.setValue(guiaForCDATA);
//Populate the Message. In here, I populate the message from a xml file
System.out.println(guiaForCDATA);
StreamSource preppedMsgSrc = new StreamSource(new StringReader(guiaForCDATA));
soapPart.setContent(preppedMsgSrc);

//Save the message
message.saveChanges();

System.out.println(message.getSOAPBody().getFirstChild().getTextContent());

SOAPMessage response = (SOAPMessage) dispatch.invoke(message);
return response;

}



if anyone can help me, I'll be very grateful! I do not know what to do to solve this mistake





I solved the problem in another way by manually filling the xml
– Gustavo Menezes
Feb 28 at 13:41





Gustavo, currently you can either delete or publish your solution for the above question
– derloopkat
Feb 28 at 13:47









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API