I'm having a very hard time creating a WSDL client, thanks to a malformed WSDL definition created using Apache Axis version 1.4 by someone else.
Let me show you the steps I'm following:
First I load up my WSDL endpoint http://xxxxx/uglySoap?wsdl on SOAP IU. The program automatically generates the following Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bal="http://xxxx/uglySoap.xsd">
<soapenv:Header/>
<soapenv:Body>
<bal:CreditRequest>
<bal:MSISDN>?</bal:MSISDN>
<bal:amountToCredit>?</bal:amountToCredit>
<!--Optional:-->
<bal:reason>?</bal:reason>
<bal:transId>?</bal:transId>
</bal:CreditRequest>
</soapenv:Body>
</soapenv:Envelope>
This request is wrong because it is missing the header (where you provide the credentials). On SOAP UI that's not a problem, I can add the missing text by hand and it will work perfectly.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:msp="http:xxxxxx/uglySoap.xsd" xmlns:bal="http://xxxxxx/uglySoap.xsd">
<soapenv:Header>
<msp:messageHeader>
<msp:trackingMessageHeader>
<msp:messageId>xxx</msp:messageId>
<msp:carrierId>xxx</msp:carrierId>
<msp:userId>xxxx</msp:userId>
<msp:password>xxxx</msp:password>
</msp:trackingMessageHeader>
</msp:messageHeader>
</soapenv:Header>
<!--from here is the same thing as before-->
<soapenv:Body>
<bal:CreditRequest>
<bal:MSISDN>xxxxx</bal:MSISDN>
<bal:amountToCredit>xxxx</bal:amountToCredit>
<!--Optional:-->
<bal:reason>xxxx</bal:reason>
<bal:transId>xxxx</bal:transId>
</bal:CreditRequest>
</soapenv:Body>
</soapenv:Envelope>
The real pain starts when I try to consume the web service on Java. The Eclipse CXF 2.7.9. tool will import the botched wsdl version without any trouble, but invoking its methods is useless since they are well... botched.
creditResponse = balanceManager.getBalanceManagement().applyCredit(creditRequest, authHeader);
JAVA ERROR: The method credit(CreditRequest, MessageHeader) is undefined for the type BalanceManagement. Really dude? You already knew this was going to happen.
So...
Please, is there any solution for Java where I could just punch my SOAP request, the URL, and get the response as an .XML file (or even as plain text, no problem!) just as SOAP UI does?
Thank you!
Ok guys, I found a working solution to make the requests by hand.
Create a new Maven project:
package main;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import io.restassured.path.xml.XmlPath;
public class Client {
public void executeRequest() {
try
{
CloseableHttpClient client = HttpClients.createDefault(); // create client
HttpPost request = new HttpPost("http://172.27.241.11:8195/mspgwservices/services/BalanceManagement"); // Create
// the
String requestXmlText = "<soapenv:Envelope ...INCLUDE YOUR XML REQUEST HERE, ON THE SAME WAY YOU WOULD DO IT ON SOAP UI... </soapenv:Envelope>";
StringEntity stringEntity = new StringEntity(requestXmlText, "utf-8");
request.addHeader("soapAction", "Credit");
request.addHeader("Accept", "text/xml");
request.addHeader("Content-Type", "text/xml;charset=utf-8");
request.setEntity(stringEntity);
CloseableHttpResponse response = client.execute(request);// Execute the command
int statusCode = response.getStatusLine().getStatusCode();
// Get the status code and assert
System.out.println("Status code: " + statusCode);
Assert.assertEquals(200, statusCode);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
// Getting the Response body
System.out.println(responseString);
XmlPath jsXpath = new XmlPath(responseString);
String textResult = jsXpath.getString("respDescription");
System.out.println("result: " + textResult );
}
catch (Exception ex)
{
System.out.println("Error." + ex.toString());
}
}
}
Also don't forget to add the following lines to your POM.XML in order to import all requred libraries
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BalanceadorLineas</groupId>
<artifactId>BalanceadorLineas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-restassured</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.scala-js</groupId>
<artifactId>scalajs-junit-test-runtime_2.11</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
Finally the pain is over! Hope it works for you too.