4

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.

Dada
  • 6,313
  • 7
  • 24
  • 43
José Ripoll
  • 504
  • 1
  • 7
  • 17
  • Unfortunately, this is too broad for SO. You can get help with your own code by providing a [mcve] and example results. –  Feb 28 '19 at 19:14
  • Take a look at apache CXF. But i dont know if it‘s compatible with java 11 yet. – Martin Frey Feb 28 '19 at 19:22
  • 1
    user1531971 First of all, thanks for the reply. Secondly, as broad as it seemed, the task i was trying to solve was simple and minimal(I think): Consuming a SOAP web service using Java 11. And although there are multiple approaches, as mentioned, most of tutorials I found, rely on tools officially deprecated for Java 11 or are some frameworks to implement the server and everything. – José Ripoll Mar 29 '19 at 22:19
  • Martin Frey, thanks for the reply. I took a look at apache CFX. It was going smoothly but then JAXB started creating multiple problems. As mentioned, for Java 11 it´s totally removed, and by doing some workarounds I was able to include it, but in a really bad way...multiple warnings, deprecated warnings, and some unstable behavior on the multiple members of my team. So I had to back out of it unfortunately – José Ripoll Mar 29 '19 at 22:26

3 Answers3

4

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.

José Ripoll
  • 504
  • 1
  • 7
  • 17
  • 1
    Thank You so much this raw solution has resolved my problem. – Piotr Żak Apr 29 '20 at 19:07
  • 1
    Glad to help! It is a raw solution but it gets things done. – José Ripoll May 07 '20 at 22:41
  • It's excellent, thanks to You I could remove whole old java ee / web services code, and my jar is light weight like a feather now :D – Piotr Żak May 08 '20 at 12:04
  • 1
    Looks like the resource has being blocked on the server (403 response). If you can, it would be great if you could share the main idea of the code. My implementation of this solution is code that I can't access anymore (i left the company i was working for), and I would like this info to be available since it worked so well for me and there aren't many resources on the topic nowadays. Thanks in advance Piotr. – José Ripoll Jun 08 '20 at 23:40
  • the link is broken, it can be accessed here: https://web.archive.org/web/20210301220843/https://technology.amis.nl/soa/how-to-call-a-call-a-webservice-directly-from-java-without-webservice-library/ – CarlosRuiz Oct 29 '21 at 14:16
0

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;
  }
}
Piotr Żak
  • 2,083
  • 6
  • 29
  • 42
0
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.

Dada
  • 6,313
  • 7
  • 24
  • 43
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 02 '21 at 16:24