Before start creating a java client application which will connect to SOAP api we first find the some basic question which is needed to understand any new technologies.
What are the web services?
Services over the internet by which two application can communicate to each other.
Then why we need it?
By web services applications which are develop in diffrent technologies can commonicate to each other. For example commonication between java and .net or php.
There is two kind of the web services
- SOAP web-services (OLD ONE)
- REST web services
In this blog we shall use SOAP web services and how we will use them in our client appliaction. But befor start first I would like to introduce some Jorgen for releted to SOAP Web services.
WSDL stand for Web Services Defination Language. It is the full defination of service over the internet in XML format.Each web service over the internet will be exposed bu WSDL.
SEI stand for Service Endpoint Interface. It is also called stub. It used to convert your request to SOAP request and also used to parse you SOAP response.
Universal Description, Discovery and Integration (UDDI) is a directory service where businesses can register and search for Web services. Taken form http://www.w3schools.com/webservices/ws_wsdl_uddi.asp
Simple Object Access Protocol. It is the message format by which data will interchange between two application and ofcourse it will be in XML fromat.
It is enough to know about to use a existing web-services. Now let do some dirty hand on code.
On web there is many web services available pick any one of them. I am using for demostration http://www.webservicex.net/ConvertTemperature.asmx?WSDL which will conver one temprature format to another one.
First we shall use WSDL to gentrate STUB(SEI). There is many tool which is available over the internet. We will use default one which come with the JDK.
Create a new directory for example sei.
mkdir sei cd sei
Now use command below command to genrate stub which will convert your request to SOAP request
wsimport -keep http://www.webservicex.net/ConvertTemperature.asmx?WSDL
-keep is command parmeter for keeping genrated source code for stub.
Create a new simple java application and make package same as STUB files package and import genrated java file inside package.
Now create a Class with main method and use this code which will convert “celsius to fahrenheit”.
System.out.print("Converting celsius to fahrenheit"); ConvertTemperature convertTemperature=new ConvertTemperature(); ConvertTemperatureSoap convertTemperatureSoap= convertTemperature.getConvertTemperatureSoap(); TemperatureUnit fromTemp=TemperatureUnit.DEGREE_CELSIUS; TemperatureUnit toTemp=TemperatureUnit.DEGREE_FAHRENHEIT; System.out.println("Temperature in Fahrenheit: "+convertTemperatureSoap.convertTemp(25.5, fromTemp, toTemp));
Let me explain above code
In the WSDL you find the
&lt;wsdl:service name="ConvertTemperature"&gt; &lt;wsdl:port name="ConvertTemperatureSoap" binding="tns:ConvertTemperatureSoap"&gt; &lt;soap:address location="http://www.webservicex.net/ConvertTemperature.asmx"/&gt; &lt;/wsdl:port&gt; &lt;/wsdl:service&gt;
In every WSDL there is wsdl:service tag which tell about the exposed web service name and having multiple wsdl:port tag which said about the class have exposed method.
So in line 2 I have created the object of Webservice and on line 3 I am trying to get object of the ConvertTemperatureSoap which have method to convert temperature from one format to another.
That’s all. I hope this blog will be helpful.
Please find the source code form https://github.com/teamjft/SOAP-web-services.
Thanks and stay tuned for how to create our own web service.