Overview
In the previous article we have gone through an overview of SOAP web service and advantage and disadvantage of it. In today’s article, we will create SOAP web service using JAX-WS libraries.
JAX-WS is a Web Services framework that provides tools and infrastructure to develop Web Services solutions for the end users and middleware developers.
There are two communication style models that are used to translate a WSDL binding to a SOAP message body. They are Document & RPC. We will discuss it in detail in coming articles. Here we will create a Web Service with RPC style.
Let’s understand steps with Stock Application, We have stock service which will give us a stock price based on the registered stock name.
Steps to Create SOAP Web Service
Let’s first create Dynamic Web Project with name ‘StockWebservice’ in eclipse.
Directory structure of Project

1. Create a Web Service Endpoint Interface
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.codenuclear; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public interface StockEndPoint { public Double getStockPrice(String stockName); } |
2. Create a Web Service Endpoint Implementation
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package com.codenuclear; import java.util.HashMap; import java.util.Map; import javax.jws.WebMethod; import javax.jws.WebService; @WebService(endpointInterface="com.codenuclear.StockEndPoint") public class StockEndPointImpl{ Map<String,Double> stockPriceMap; /* * Creating a static stock data. */ public StockEndPointImpl() { stockPriceMap=new HashMap<String, Double>(); stockPriceMap.put("Amazon", 1100d); stockPriceMap.put("Google", 2100d); stockPriceMap.put("Apple", 1400d); } /* * exposed method, Which can be invoked by client. */ @WebMethod(operationName="getStockPrice") public Double getStockPrice(String stockName){ return stockPriceMap.get(stockName); } } |
3. Create a web service deployment descriptor
0 1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="StockEndPoint" implementation="com.codenuclear.StockEndPointImpl" url-pattern="/stockendpoint"/> </endpoints> |
When user access
4. Create a standard web.xml deployment descriptor
Within web deployment descriptor we defines
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>StockEndPoint</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StockEndPoint</servlet-name> <url-pattern>/stockendpoint</url-pattern> </servlet-mapping> <session-config> <session-timeout>100</session-timeout> </session-config> </web-app> |
5. JAX-WS Dependencies
By default Tomcat does not come with JAX-WS dependent libraries, So, We have to include it manually.
Dependencies can be download from here
Add listed jars in tomcat lib folder or add it in web service class-path.
6. Deploy Web Service.
Our service is ready, Now lets deploy it in Apache Container. Click on Project and click Run -> Run on Server.
From the service name, Our endpoint URL will be.
http://localhost:8080/StockWebservice/stockendpoint
For testing we can access endpoint URL, If we see the following a page that means service deployed successfully.

And WSDL URL
http://localhost:8080/StockWebservice/stockendpoint?wsdl
Web Service Client
Our service is ready to test, There are many ways to test SOAP Web service.
1. SoapUI
SoapUI is a third party tool to invoke endpoint. Listed are steps to invoke SOAP web service from SoapUI.
1. Create SOAP project with Project name and WSDL URL.

2. Click on request and change input param in soap envelope and run it.

2. Java Web Service Client
Let’s write client code in Java. Below is a client code to invoke SOAP web service from Java.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.codenuclear.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.codenuclear.StockEndPoint; public class StockEndPointClient { public static void main(String[] args) throws MalformedURLException { URL url = new URL("http://localhost:8080/StockWebservice/stockendpoint?wsdl"); QName qname = new QName("http://codenuclear.com/", "StockEndPointImplService"); Service service = Service.create(url, qname); StockEndPoint stockEndPoint = service.getPort(StockEndPoint.class); System.out.println(stockEndPoint.getStockPrice("Apple")); } } |
Output
That’s all for SOAP Web Service, Hope you like it, Keep reading and sharing !! :)