In one of our earlier tutorial we have seen Web Services in Java and we have discussed about SOAP Web Service and RESTful Web Services along with their differences. As further we will see a simple example for how to create Web Service server and client using SOAP. For this we will use eclipse J2EE IDE and Apache Tomcat as web server.
First we will create simple Server which will take 2 numbers as input and returns the addition of the program. Here by using Eclipse its very simple to create a service and to deploy and publish a service. Everything we can do it just by clicks rather than doing it lot manually. And also using IDE's will make life more easy than manually.
Server Program:
STEP - 1 : Create a Dynamic Web Project (File -> New -> Dynamic Web Project). Create your project by setting your Target run-time server to installed Apache Tomcat web server. In demo I have used Apache Tomcat 7.0 and you can decide your version according to your need.
STEP - 2 : Create a new class file under Java Resource by giving package and class name. Then implement the server side method to publish.
STEP - 3 : After finishing the server side method implementation, select New -> Others and type "web" in Wizards, where you can see Web Service under Web Services list.
STEP - 4 : Click next and select the class which you implemented the method to publish under Service implementation drop down box. As already we are ready with Develop service, so we have to
Assemble service
Deploy service
Install service
Start service
Test service
to make Web Service server ready with publishing the service which we have implemented. So that just passing the WSDL (Web Services Description Language) to client they can understand the server implementation and can consume our service easily. Basically its XML which used to describe the functionality offered or published by a web service. For more details on WSDL you can refer to WSDL wiki.
STEP - 5 : Our Web Service server created successfully and we are ready to test our service. In next screen you can see SOAP Binding and service created with WSDL. You can pass input parameter and can test the service as below
And also we can see SOAP request and response by switching from form to source in result tab as below.
STEP - 6 : Next you can check your WSDL file have created correctly or not in your web browser by passing. http://localhost:8080/MyServer/services/MyClass?wsdl
We have done with server implementation by creating the service and publishing with the help of Eclipse of IDE. Next important that we are going to localhost as our web server so we should not stop the server which running. In real-time we will deploy our service in some web servers and we will access our service through internet. Where as for our demo we are localhost for both server and client.
Next go-head and create the client and consume the service which we have published above. For that we need to create the client application given below.
Client Program:
STEP - 7 : Create a Dynamic Web Project (File -> New -> Dynamic Web Project).
STEP - 8 : Next we need to create stub from WSDL file. For this server where we have published need to be running and as discussed above we haven't stopped the server which we have started while creating our server.
For creating Web Service Client we need to select New -> Other and type "web" in the Wizards as given below and select the Web Service Client under Web Services tab.
STEP - 9 : Click Next and pass the WSDL url in Service definition text box and set as Test client and click Finish which will read the WSDL file and generate the java stub code.
Once you finished we can see the Java stub created along with Service and Proxy classes. Next we can see the list of method(s) published by server. By selecting the method we can test directly from eclipse by giving inputs as below image.
STEP - 10 : Next we need to write a client program to invoke the remote method using SOAP Web Services. Create new class called MyClient as below image.
STEP - 11 : Just implement the client by using endpoint URL and service instance and test your Web Service using SOAP.
No comments:
Write comments