How to Build and Deploy Web Service and Client in Java

Web Services are most widely implemented examples of Service Oriented Architecture (SOA). A service contract is defined with the help of SOAP and Web Service Definition Language (WSDL) and that is published for other applications to use. In this article we will see how we can create web service and web service client in java.

Software used in this example

  • Java 6
  • Eclipse
  • Apache Tomcat

Lets create a “HelloServer” project for our Web Service. This project will act as our server who will provide the service. In your eclipse IDE select New -> Project -> Dynamic Web Project. Put in the name for project as below.Eclipse - New Web Project

We need to add web services capability to our project. For that we need to do some configuration. In above screen click on the Modify button for configuration. Below screen will appear.

Web Project Facet Configuation

In above configuration make sure the Axis2 Web Service option is checked along with others. We need to have Dynamic Web Module version as 2.5. Also in Runtime tabs please select Runtime as Apache Tomcat 6.0 and Above. Click Ok. You will be redirected back to previous screen, there click on Finish with taking default options for other configurations and paths.

Once you are done, you can see the your project in eclipse IDE as below. Post this we will add a sample class We can always add a package first and then class to it but for tutorial purpose lets add it to base package directly. Eclipse - Add New Class

Below is sample code that can be put in to our newly added class.

To convert our class in to web service we need to do following steps –

  1. Click on File -> New – > Web Service or You can use shortcut to open Web Service and choose Web Service Option. Eclipse - Adding Web Service
  1. In  Server Implementation select the name of the class as MyHelloWebService … If you didn’t find name of the class click on Browse and then select the name of the class from the list.
  2. Select Server Runtime :   The instance of Tomcat Server you are using for developing web services
  3. Web Service Runtime : Apache Axis2 ( note you should have axis2-1.6.3 exe or you can download it from internet)

Eclipse - Adding Target Runtime

  1. Click on Ok -> Next -> Click on Next and then Start Server
  2. The Tomcat server will be starting here. Then Click on Finished
  3. Eclipse will show you the list of available instance of server with state

Once this is done our server project will be deployed to our local tomcat and we can access that on port 8080 Web Services Project Deployed

The Services link will have all the services that are deployed as part of our HelloServer project. If you click it, you will get the list as Web Services - List

As mentioned earlier, the web service contract is published as wsdl which can be accessed via a url by appending ?wsdl at the end of our service endpoint. In our case the url will be as http://localhost:8080/HelloServer/services/MyHelloWebService?wsdl  Web Services - WSDL

Now our Server is ready and we have published the wsdl. Lets prepare our web service client. We need to create one more dynamic web project named “HelloServiceClient”. Please follow the same steps we did for our server project. To add the capability of accessing web service to our project we need to generate web service stub code in our newly created client project. In order to do that, please follow steps given below

  1. Click on File-> New-> Others (Cntr+N) -> Web Service Client Web Service Client
  2. Click Next. In the next screen copy and paste the wsdl of our web service Web Service Client - Configuration
  3. Click on Server Runtime : and Select any  Available instance of Tomcat server
  4. Click on Web Service runtime: Apache Axis2
  5. Click on Next following dialog window will appear. Config
  6. Click on Finish. Now your Web client is linked with  Web Service  definition

Now we will add a new class to our client project that will actually try and call the web service. Remember when you did above steps the necessary stub classes are generated in our client project which we can directly use. We will add a new class as below which accepts an input from user and sends it to the service via call.

Hope above server and client example helps you.

  1. pradeep
    May 14, 2018 | Reply
  2. vipin
    October 23, 2017 | Reply
  3. Dr. Pallavi Kulkarni
    January 30, 2017 | Reply
    • Renuka
      February 3, 2017 | Reply
  4. Vijaya ghewari
    January 30, 2017 | Reply
    • Renuka
      February 3, 2017 | Reply
  5. Ashok Shinde
    January 29, 2017 | Reply
    • Renuka
      February 3, 2017 | Reply

Add a Comment

Your email address will not be published. Required fields are marked *