I'm trying to make a soap request to a Brazilian government endpoint and I'm facing some trouble.
They make the following wsdl available: https://mdfe-homologacao.svrs.rs.gov.br/ws/MDFeRecepcaoSinc/MDFeRecepcaoSinc.asmx?wsdl
I then generated the corresponding stub using wsimport tool, which consists on the following:
Then, on my Java application, I did the following:
ObjectFactory of = new ObjectFactory();
JAXBElement<String> jaxb = of.createMdfeDadosMsg("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:mdf=\"http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc\"><soap:Header/><soap:Body><mdf:mdfeDadosMsg>?</mdf:mdfeDadosMsg></soap:Body></soap:Envelope>");
MDFeRecepcaoSinc recepcao = new MDFeRecepcaoSinc();
MDFeRecepcaoSincSoap12 soap = recepcao.getMDFeRecepcaoSincSoap12(
// new AddressingFeature(true),
// new MTOMFeature(false),
// new RespectBindingFeature(true)
);
System.out.println(soap.mdfeRecepcao(jaxb.getValue()).getContent());
Although the only result I'm getting, independent of the body text, is [[retMDFe: null]]
.
I managed to make it work on SoapUI with this exact same request envelope and it returns a correct xml with a few tags inside retMDFe
.
It appears to be connecting to their server from my Java client since the tag retMDFe
isn't present in the WSDL file or any stub I generated, and since I don't receive the 403 - Forbidden
error anymore (configured the system keystore correctly).
Unfortunately, this webservice only allows connections issued with a digital certificate.
I'm suspecting the error may be from the mapping from the endpoint to the MdfeRecepcaoResult class. I've tried a few things:
Client received SOAP Fault from server: Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1. Please see the server log to find more detail regarding exact cause of the failure.
;[[retMDFe: null]]
response;What am I possibly doing wrong here? Thank you for your time!
Edit 1:
/**
*
* @param mdfeDadosMsg
* @return
* returns br.inf.portalfiscal.mdfe.wsdl.mdferecepcaosinc.MdfeRecepcaoResult
*/
@WebMethod(action = "http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc/mdfeRecepcao")
@WebResult(name = "mdfeRecepcaoResult", targetNamespace = "http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc", partName = "mdfeRecepcaoResult")
public MdfeRecepcaoResult mdfeRecepcao(
@WebParam(name = "mdfeDadosMsg", targetNamespace = "http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc", partName = "mdfeDadosMsg")
String mdfeDadosMsg);
/**
* Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc", name = "mdfeDadosMsg")
public JAXBElement<String> createMdfeDadosMsg(String value) {
return new JAXBElement<String>(_MdfeDadosMsg_QNAME, String.class, null, value);
}
Edit 2:
wsimport version: wsimport version "2.2.9"
wsimport generated files:
br/inf/portalfiscal/mdfe/wsdl/mdferecepcaosinc/MDFeRecepcaoSinc.java
br/inf/portalfiscal/mdfe/wsdl/mdferecepcaosinc/MDFeRecepcaoSincSoap12.java
br/inf/portalfiscal/mdfe/wsdl/mdferecepcaosinc/MdfeRecepcaoResult.java
br/inf/portalfiscal/mdfe/wsdl/mdferecepcaosinc/ObjectFactory.java
br/inf/portalfiscal/mdfe/wsdl/mdferecepcaosinc/package-info.java
I use the following to produce the stubs: wsimport -extension -keep -verbose MDFeRecepcaoSinc.wsdl
and it only gives a single warning: [WARNING] a porta SOAP \"MDFeRecepcaoSincSoap12\": usa um bind de SOAP 1.2 não padrão. linha 40 de file:/home/teste-progra/tiago/backup/mdfe/wsimport-test/MDFeRecepcaoSinc.wsdl
(which means that the port used by the web service does not use a conventional (or default) bind for SOAP 1.2, and has to do with the following line in the wsdl:
<wsdl:port name="MDFeRecepcaoSincSoap12" binding="tns:MDFeRecepcaoSincSoap12">
I'm not sure if that's of any use though, hence the connection is effectively being held on.
Edit 3: I'm able to successfully read the HTTP request and response with System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true");
and its content is the following:
---[HTTP request - https://mdfe.svrs.rs.gov.br/ws/MDFeRecepcaoSinc/MDFeRecepcaoSinc.asmx]---
Accept: application/soap+xml, multipart/related
Content-Type: application/soap+xml; charset=utf-8;action="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc/mdfeRecepcao"
User-Agent: JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e
<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Body><mdfeDadosMsg xmlns="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc"><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:mdf="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc"><soap:Header/><soap:Body><mdf:mdfeDadosMsg>?</mdf:mdfeDadosMsg></soap:Body></soap:Envelope></mdfeDadosMsg></S:Body></S:Envelope>--------------------
---[HTTP response - https://mdfe.svrs.rs.gov.br/ws/MDFeRecepcaoSinc/MDFeRecepcaoSinc.asmx - 200]---
null: HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Length: 586
Content-Type: application/soap+xml; charset=utf-8
Date: Fri, 27 Sep 2024 18:54:54 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><mdfeRecepcaoResult xmlns="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc"><retMDFe xmlns="http://www.portalfiscal.inf.br/mdfe" versao="3.00"><tpAmb>1</tpAmb><cUF>43</cUF><verAplic>RS20240710093839</verAplic><cStat>244</cStat><xMotivo>Rejeição: Falha na descompactação da área de dados</xMotivo></retMDFe></mdfeRecepcaoResult></soap:Body></soap:Envelope>--------------------
So I'm actually receiving content under retMDFe
tag! I just can't get it mapped properly to MdfeRecepcaoResult.
I've tried setting the WS return as String so I could print it directly, which gave me an empty result.
Per instance, which class actually implements this interface?
You are not correctly marshalling the element here.
// Create the ObjectFactory
ObjectFactory of = new ObjectFactory();
// Create the JAXBElement with the correct inner XML content
String innerXmlContent = "<mdfeDadosMsg xmlns=\"http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoSinc\">...</mdfeDadosMsg>";
JAXBElement<String> jaxb = of.createMdfeDadosMsg(innerXmlContent);
// Create the service and get the SOAP port
MDFeRecepcaoSinc recepcao = new MDFeRecepcaoSinc();
MDFeRecepcaoSincSoap12 soap = recepcao.getMDFeRecepcaoSincSoap12(
new AddressingFeature(true),
new MTOMFeature(false),
new RespectBindingFeature(true)
);
MdfeRecepcaoResult response = soap.mdfeRecepcao(jaxb.getValue());
List<Element> list = response.getContent();
for(Element element : list) {
System.out.println(element.getTextContent());
}