Hi!
I want to share a usefull way to call a SOAP Web Service from Mathematica within Java.
Basically it is about generating a client with Java with which to call a remote Web Service. This client will be represented by Java .class files that can be used by Mathematica, so you can retrive from the WS data for computation, for example, or perfoming other operations.
There is a fantastic package with which you can link Mathematica and Java, and then call up the Java functions from Mathematica ( and Wolfram functions from Java ) : JLink.
Once you have .class files you can call back them into a Wolfram session.
Furthermore there is a Java tool, wsimport ( from Java 7 and above ), with which you can generate a client for a WS, and so the .class files. All steps, including the client generation through wsimport will be performed from Mathematica.
We will use a free and simple WS that perform basic calculus for demonstration. The wsdl url is: http://www.dneonline.com/calculator.asmx?wsdl.
Here is a short scheme from WS to Mathematica:
And this is the scheme to genarate the Java client:
First step, generate the java client (it suppose you have java >= 1.7 in your system); choose a directory ( in this case C:\Users\Utente\Desktop\JavaLink\wsdl), choose the packaging ( in this case com.calculator.client ) and a name for the jar (client.jar) containing the .class client files:
process = RunProcess[$SystemShell, All, "
cd C:\\Users\\Utente\\Desktop\\JavaLink\\wsdl
wsimport -p com.calculator.client http://www.dneonline.com/calculator.asmx?wsdl -clientjar client.jar
exit
"]
Now visualize the files inside the jar rapresenting the client:
Map[FileNameTake[#] &,
FileNames[".class", "C:\\Users\\Utente\\Desktop\\JavaLink\\wsdl",
Infinity]] // TableForm
The result is:
Link Mathematica and Java and add the client.jar to the class path (be sure JLink points to a java version >=1.7):
Needs["JLink`"];
AddToClassPath[
"C:\\Users\\Utente\\Desktop\\JavaLink\\wsdl\\client.jar"];
ReinstallJava[
CommandLine ->
"C:\\Program Files\\Java\\jre1.8.0_191\\bin\\java.exe"]
You can explore methods and constructors of every class by using:
Methods["com.calculator.client.ClassName"]
Constructors["com.calculator.client.ClassName"]
Now build the URL object:
url = JavaNew["java.net.URL", "http://www.dneonline.com/calculator.asmx?wsdl"]
From URL object build the calculator object:
calculator = JavaNew["com.calculator.client.Calculator", url]
And so the soapCalculator object:
soapCalculator = calculator@getCalculatorSoap[]
Now we are able to use the client ws methods and perform some basic operation:
Gianluca