Soap Webservices in java can be developed in may ways. We learned about JAX-WS SOAP Web Services in our last tutorial, today we will learn how we can create SOAP web service and it’s client program using Eclipse. Here we will not use JAX-WS, we will be using Apache Axis that is integrated in the Eclipse and provide quick and easy way to transform a application into Java Web Service and create client stubs with test JSP page for testing purpose.

SOAP Webservices in Java

I am using Eclipse Mars Release (4.5.0) for this tutorial but I think these steps will work with older versions of eclipse too. Also make sure you have added Apache Tomcat or any other servlet container as server in the Eclipse. Let’s start with our Eclipse Web Service implementation now.

SOAP Web Service Example

Let’s get started with our SOAP web service example in Eclipse. First of all we will create a simple Dynamic Web Project in Eclipse that will contain the business logic for our application.

1-Create-Dynamic-Web-Projec

Click on Next button above and you will get next page to provide your web project name and Target Runtime. Notice that I am using Apache Tomcat 8, you can use any other standard servlet container too.

18-SOAP-Web-Service-WSDL

Click on Next and you will be asked to provide “Context Root” and Content Directory location. You can leave them as default.

18-SOAP-Web-Service-WSDL

Click on Finish and Eclipse will create the project skeleton for you. Let’s get started with our business logic. So for our example, we would like to publish a web service that can be used to add/delete/get an object. So first step is to create a model bean.

Notice that above is a simple java bean, we are implementing Serializable interface because we will be transporting it over the network. We have also provided toString method implementation that will be used when we will print this object at client side.

Next step is to create service classes, so we will have an interface as PersonService and it’s simple implementation class PersonServiceImpl.

Below is the implementation service class, we are using Map to store Person objects as data source. In real world programming, we would like to save these into database tables.

That’s it for our business logic, since we will use these in a web service, there is no point of creating web pages here. Notice that we have no reference to any kind of web services classes in above code.

SOAP Webservices in Java using Eclipse

Once our business logic is ready, next step is to use Eclipse to create a web service application from this. Create a new project and select Web Service wizard.

Click Next button and you will get a page where web service and it’s client details have to be provided. This is the most important page in creating web service. Make sure you select “Web Service type” as “Bottom up Java bean Web Service” because we are implementing with bottom up approach. There are two ways to create web service:

  1. Contract last or Bottom up approach: In this approach we first create the implementation and then generate the WSDL file from it. Our implementation fits in this category.
  2. Contract first or Top Down Approach: In this approach, we first create the web service contract i.e. WSDL file and then create the implementation for it.
  3. 18-SOAP-Web-Service-WSDL

In the service implementation, provide the implementation class PersonServiceImpl fully classified path. Make sure you move the slider in service and client type to left side so that it can generate client program and also UI to test our web service. Check for the configurations in web service implementation, you should provide correct details for Server runtime, Web service runtime and service project. Usually they are auto populated and you don’t need to make any changes here.

For client configurations, you can provide the client project name as you like. I have left it to default as SOAPExampleClient. If you will click on the link for web service runtime, you will get different options as shown in below image. However I have left it as the default one.

Click on Next button and then you will be able to choose the methods that you want to expose as web service. You will also be able to choose the web service style as either document or literal. You can change the WSDL document name but it’s good to have it with implementation class name to avoid confusion later on.

7-WSDL-Methods-Style

Click on Next button and you will get server startup page, click on the “Start server” button and then next button will enable.

Click on Next button and you will get a page to launch the “Web Services Explorer”.

9-Web-Services-Explorer-450x297

Click on Launch button and it will open a new window in the browser where you can test your web service before moving ahead with the client application part. It looks like below image for our project.

18-SOAP-Web-Service-WSDL

We can do some sanity testing here, but for our simple application I am ready to go ahead with client application creation. Click on the Next button in the Eclipse web services popup window and you will get a page for source folder for client application.

Click on Next button and you will get different options to choose as test facility. I am going ahead with JAX-RPC JSPs so that client application will generate a JSP page that we can use.

18-SOAP-Web-Service-WSDL

Notice the methods getEndpoint() and setEndpoint(String) added that we can use to get the web service endpoint URL and we can set it to some other URL in case we move our server to some other URL endpoint.

Click on Finish button and Eclipse will create the client project in your workspace, it will also launch client test JSP page as shown below.

18-SOAP-Web-Service-WSDL

You can copy the URL and open in any browser you would like. Let’s test some of the services that we have exposed and see the output.

Eclipse SOAP Web Service Test

  • addPerson
  • getPerson
  • 18-SOAP-Web-Service-WSDL
  • getAllPersons
  • 18-SOAP-Web-Service-WSDL
    Notice that Person details are not printed in the results section, this is because it’s auto generated code and we need to refactor it a little to get the desired output.

Open Result.jsp in the client project and you will see it’s using switch case to generate the result output. For getAllPersons() method, it was case 42 in my case. Note that it could be totally different in your case. I just changed the code for case 42 as shown below.

After that we get below output, note that Eclipse is doing hot deployment here, so I didn’t had to redeploy my application.

 

  • 18-SOAP-Web-Service-WSDL

 

So it looks like our web service and client applications are working fine, make sure to spend some time in looking at the client side stubs generated by Eclipse to understand more.

SOAP Web Service WSDL and Configs

Finally you will notice that WSDL file is generated in the web service project as below.

PersonServiceImpl.wsdl code:

If you will open it in design mode in Eclipse, it will look like below image.

18-SOAP-Web-Service-WSDL

You can also access web service WSDL file through browser by appending ?wsdl to the web service endpoint.

18-SOAP-Web-Service-WSDL

You will also note that web.xml is modified to use Apache Axis as front controller for web service.

Below image shows the web service and client project with all the auto generated stubs and JSP pages to test the web service.

JAXRS-Jersey-Project

That’s all for soap webservices in java example using Eclipse, as you can see that all the hard part was done by Eclipse automatically and all our focus was to write business logic for our web service.

By admin

Leave a Reply