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.
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.
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. 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. Click on Next and you will be asked to provide “Context Root” and Content Directory location. You can leave them as default. 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.
package com.journaldev.jaxws.beans;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = -5577579081118070434L;
private String name;
private int age;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString(){
return id+"::"+name+"::"+age;
}
}
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
.
package com.journaldev.jaxws.service;
import com.journaldev.jaxws.beans.Person;
public interface PersonService {
public boolean addPerson(Person p);
public boolean deletePerson(int id);
public Person getPerson(int id);
public Person[] getAllPersons();
}
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.
package com.journaldev.jaxws.service;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.journaldev.jaxws.beans.Person;
public class PersonServiceImpl implements PersonService {
private static Map<Integer,Person> persons = new HashMap<Integer,Person>();
@Override
public boolean addPerson(Person p) {
if(persons.get(p.getId()) != null) return false;
persons.put(p.getId(), p);
return true;
}
@Override
public boolean deletePerson(int id) {
if(persons.get(id) == null) return false;
persons.remove(id);
return true;
}
@Override
public Person getPerson(int id) {
return persons.get(id);
}
@Override
public Person[] getAllPersons() {
Set<Integer> ids = persons.keySet();
Person[] p = new Person[ids.size()];
int i=0;
for(Integer id : ids){
p[i] = persons.get(id);
i++;
}
return p;
}
}
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.
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:
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. 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”. 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. 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. 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. 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.
getAllPersons 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.
case 42:
gotMethod = true;
com.journaldev.jaxws.beans.Person[] getAllPersons42mtemp = samplePersonServiceImplProxyid.getAllPersons();
if(getAllPersons42mtemp == null){
%>
<%=getAllPersons42mtemp %>
<%
}else{
String tempreturnp43 = null;
if(getAllPersons42mtemp != null){
java.util.List<com.journaldev.jaxws.beans.Person> listreturnp43= java.util.Arrays.asList(getAllPersons42mtemp);
//tempreturnp43 = listreturnp43.toString();
for(com.journaldev.jaxws.beans.Person p : listreturnp43){
int id = p.getId();
int age = p.getAge();
String name=p.getName();
%>
<%=id%>::<%=name %>::<%=age %>
<%
}
}
}
break;
After that we get below output, note that Eclipse is doing hot deployment here, so I didn’t had to redeploy my application.
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.
Finally you will notice that WSDL file is generated in the web service project as below. PersonServiceImpl.wsdl code:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="https://service.jaxws.journaldev.com" xmlns:apachesoap="https://xml.apache.org/xml-soap" xmlns:impl="https://service.jaxws.journaldev.com" xmlns:intf="https://service.jaxws.journaldev.com" xmlns:tns1="https://beans.jaxws.journaldev.com" xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="https://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="https://service.jaxws.journaldev.com" xmlns="https://www.w3.org/2001/XMLSchema">
<import namespace="https://beans.jaxws.journaldev.com"/>
<element name="addPerson">
<complexType>
<sequence>
<element name="p" type="tns1:Person"/>
</sequence>
</complexType>
</element>
<element name="addPersonResponse">
<complexType>
<sequence>
<element name="addPersonReturn" type="xsd:boolean"/>
</sequence>
</complexType>
</element>
<element name="deletePerson">
<complexType>
<sequence>
<element name="id" type="xsd:int"/>
</sequence>
</complexType>
</element>
<element name="deletePersonResponse">
<complexType>
<sequence>
<element name="deletePersonReturn" type="xsd:boolean"/>
</sequence>
</complexType>
</element>
<element name="getPerson">
<complexType>
<sequence>
<element name="id" type="xsd:int"/>
</sequence>
</complexType>
</element>
<element name="getPersonResponse">
<complexType>
<sequence>
<element name="getPersonReturn" type="tns1:Person"/>
</sequence>
</complexType>
</element>
<element name="getAllPersons">
<complexType/>
</element>
<element name="getAllPersonsResponse">
<complexType>
<sequence>
<element maxOccurs="unbounded" name="getAllPersonsReturn" type="tns1:Person"/>
</sequence>
</complexType>
</element>
</schema>
<schema elementFormDefault="qualified" targetNamespace="https://beans.jaxws.journaldev.com" xmlns="https://www.w3.org/2001/XMLSchema">
<complexType name="Person">
<sequence>
<element name="age" type="xsd:int"/>
<element name="id" type="xsd:int"/>
<element name="name" nillable="true" type="xsd:string"/>
</sequence>
</complexType>
</schema>
</wsdl:types>
<wsdl:message name="addPersonResponse">
<wsdl:part element="impl:addPersonResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getAllPersonsResponse">
<wsdl:part element="impl:getAllPersonsResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="deletePersonResponse">
<wsdl:part element="impl:deletePersonResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="addPersonRequest">
<wsdl:part element="impl:addPerson" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getPersonResponse">
<wsdl:part element="impl:getPersonResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getPersonRequest">
<wsdl:part element="impl:getPerson" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="deletePersonRequest">
<wsdl:part element="impl:deletePerson" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getAllPersonsRequest">
<wsdl:part element="impl:getAllPersons" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="PersonServiceImpl">
<wsdl:operation name="addPerson">
<wsdl:input message="impl:addPersonRequest" name="addPersonRequest">
</wsdl:input>
<wsdl:output message="impl:addPersonResponse" name="addPersonResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="deletePerson">
<wsdl:input message="impl:deletePersonRequest" name="deletePersonRequest">
</wsdl:input>
<wsdl:output message="impl:deletePersonResponse" name="deletePersonResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPerson">
<wsdl:input message="impl:getPersonRequest" name="getPersonRequest">
</wsdl:input>
<wsdl:output message="impl:getPersonResponse" name="getPersonResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getAllPersons">
<wsdl:input message="impl:getAllPersonsRequest" name="getAllPersonsRequest">
</wsdl:input>
<wsdl:output message="impl:getAllPersonsResponse" name="getAllPersonsResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PersonServiceImplSoapBinding" type="impl:PersonServiceImpl">
<wsdlsoap:binding style="document" transport="https://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="addPerson">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="addPersonRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="addPersonResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="deletePerson">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="deletePersonRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="deletePersonResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPerson">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getPersonRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getPersonResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getAllPersons">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getAllPersonsRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getAllPersonsResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PersonServiceImplService">
<wsdl:port binding="impl:PersonServiceImplSoapBinding" name="PersonServiceImpl">
<wsdlsoap:address location="https://localhost:8080/SOAPExample/services/PersonServiceImpl"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
If you will open it in design mode in Eclipse, it will look like below image. You can also access web service WSDL file through browser by appending ?wsdl to the web service endpoint. You will also note that web.xml is modified to use Apache Axis as front controller for web service.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SOAPExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet>
<display-name>Axis Admin Servlet</display-name>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
</web-app>
Below image shows the web service and client project with all the auto generated stubs and JSP pages to test the web service. 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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Hi pankaj This tutorial is really helpful, but I want any examples with google web toolkit with soap, Will you provide some hint or tutorial on GWT application I. E rpc call with soap service How soap service can be call in gwt application
- Himmat
Using the current eclipse version (201912), I am unable to create a client. Tried it two times following different paths after launching the service, but didn’t get a form to create the client as described.
- dklein
Hi Pankaj, a clear and excellent intro. Thank you, Csaba
- Csaba Botos
How can my rest web service consume a soap client request
- Sas
I am getting the exception after trying to add a peson in webservice: Exception: java.lang.InstantiationException: com.amadeus.ocg.standard.access.newwebservice.PersonServiceImpl Message: java.lang.InstantiationException: com.amadeus.ocg.standard.access.newwebservice.PersonServiceImpl PersonServiceImpl is an interface by default. How can I correct this issue?
- Harshit
Excellent Example
- Vijay
how to make soap webservices to use rest api url
- Saurabh
Well i cant really get the code right for showing all persons. Heres the code: case 42: gotMethod = true; com.model.VO.Person[] getallPerson42mtemp = samplePersonServiceImplProxyid.getallPerson(); if(getallPerson42mtemp == null){ %> <% }else{ String tempreturnp43 = null; if(getallPerson42mtemp != null){ java.util.List listreturnp43= java.util.Arrays.asList(getallPerson42mtemp); tempreturnp43 = listreturnp43.toString(); } for(com.model.VO.Person p : listreturnp43){ int id = p.getId(); int age = p.getAge(); String name=p.getName(); %> :::: <% } } In the for loop, its giving me error on listreturnp43 saying it cannot be resolved to a variable. I did exactly what you illustrated. Any suggeestions?
- Neel Nagan
I have made the changes accordingly for gellAllPersons in result.jsp case 15: gotMethod = true; com.nttdata.model.Person[] getAllPersons15mtemp = samplePersonServiceIMPLProxyid.getAllPersons(); if(getAllPersons15mtemp == null){ %> <% }else{ String tempreturnp16 = null; if(getAllPersons15mtemp != null){ java.util.List listreturnp16= java.util.Arrays.asList(getAllPersons15mtemp); //tempreturnp16 = listreturnp16.toString(); for(com.nttdata.model.Person p : listreturnp16){ int id = p.getId(); int age = p.getAge(); String name=p.getName(); %> :::: <% } } } still it is returning null. Please help me out
- abhinay
How consume oracle on demand crm using soap web service in java using apache axis 1.4 with authentication.
- Raj Singh