José Ripoll
José Ripoll

Reputation: 554

SOAP client on Java 11

I need to consume a SOAP service, and I have seen on the spring tutorial that my java classes for consuming and receiving the services, can be automatically generated using a tool or a framework.

The thing is most tutorials rely on wsimport tool from the JDK...and after lots of hours trying I found out that for Java 11, this is deprecated. After this I found this ,this, and this talking about some workarounds for this problem. I tried all of them, but my gradle.build starts generating dependencies issues around this libraries. I have tried to exclude the problematic libraries but it doesn´t solve the issue.

So I'm wondering how can I generate my SOAP client classes on a not so patched way?

Additional info: It's a contract first approach, the service is on the web and it is a ?wsdl url.

Upvotes: 7

Views: 14844

Answers (3)

Sudhakar Rathi
Sudhakar Rathi

Reputation: 11

public BlnInitBookData initTrans(String ccode, String license) {
BlnInitBookData initBookData = null;
try {

    BlnInitBook request = new BlnInitBook();

    request.setLicenseType(license);
    request.setStrCinemaCode(ccode);

    initBookData = ((BlnInitBookResponse) getWebServiceTemplate().marshalSendAndReceive(hosted_server_URL, request,
            new SoapActionCallback("URL_of_SOAP_api"))).getServiceResponse1()
        .getBlnInitBookData();

} catch (final Exception e) {
    logger.error(this.getClass().getName() + e.getMessage);
}
return initBookData;}

In Java, use the WebServiceGatewaySupport class, It worked for me. I generated SOAP requests, response classes.

Upvotes: 0

Piotr Żak
Piotr Żak

Reputation: 2413

ofcourse that I can share :) my coding:

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class GusGetCompanyRawXml {

  public String getCompanyRawXmlData(String sessionKey, String polishVatId) {
    String outputString = "";
    try {
      URL url = new URL("https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc");
      URLConnection connection = url.openConnection();
      HttpURLConnection httpConn = (HttpURLConnection) connection;
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      String xmlInput =
          "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"\n"
              + "xmlns:ns=\"http://CIS/BIR/PUBL/2014/07\" xmlns:dat=\"http://CIS/BIR/PUBL/2014/07/DataContract\">\n"
              + "<soap:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">\n"
              + "<wsa:To>https://wyszukiwarkaregontest.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc</wsa:To>\n"
              + "<wsa:Action>http://CIS/BIR/PUBL/2014/07/IUslugaBIRzewnPubl/DaneSzukajPodmioty</wsa:Action>\n"
              + "</soap:Header>\n"
              + "<soap:Body>\n"
              + "<ns:DaneSzukajPodmioty>\n"
              + "<ns:pParametryWyszukiwania>\n"
              + "<dat:Nip>"+polishVatId+"</dat:Nip>\n"
              + "</ns:pParametryWyszukiwania>\n"
              + "</ns:DaneSzukajPodmioty>\n"
              + "</soap:Body>\n"
              + "</soap:Envelope>";

      byte[] buffer;
      buffer = xmlInput.getBytes();
      bout.write(buffer);
      byte[] b = bout.toByteArray();
      String SOAPAction = "http://CIS/BIR/PUBL/2014/07/IUslugaBIRzewnPubl/Zaloguj";

      httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
      httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
      httpConn.setRequestProperty("SOAPAction", SOAPAction);
      httpConn.setRequestProperty("sid", sessionKey);
      httpConn.setRequestMethod("POST");
      httpConn.setDoOutput(true);
      httpConn.setDoInput(true);
      OutputStream out = httpConn.getOutputStream();
//Write the content of the request to the outputstream of the HTTP Connection.
      out.write(b);
      out.close();
//Ready with sending the request.

//Read the response.
      InputStreamReader inputStreamReader = new InputStreamReader(httpConn.getInputStream(), "UTF-8");
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//
      String responseString = "";

//Write the SOAP message response to a String.
      while ((responseString = bufferedReader.readLine()) != null) {
        if (StringUtils.contains(responseString, "&lt;")) {
          String unescapedString = StringEscapeUtils.unescapeXml(responseString);
          String remove = StringUtils.remove(unescapedString, "\r");
          outputString = outputString + remove;
        }
      }
    } catch (IOException e){
      log.error("Get customer data from gus failed",e.getStackTrace());
    }
    return outputString;
  }
}

Upvotes: 0

Jos&#233; Ripoll
Jos&#233; Ripoll

Reputation: 554

At the end, I just followed this tutorial, which was simple enough and allowed me to consume a SOAP web service and then build an XML file to process the info retrieved. Hopefully Java 11 will have some better support for this type of service on the near future, but meanwhile I solved my problem and maybe this post can be useful to someone with a similar task to perform.

Upvotes: 5

Related Questions