How to create SOAP based web service in Java

JAXWS API is a technology for building web services and clients that communicate using XML.It allows developers to write message-oriented as well as Remote Procedure Call-oriented (RPC-oriented) web services.

JAX-WS specification provides annotation which are used by web service programmer to annotate service class,interfaces,methods and parameters.Different software vendors including Sun Microsystem provides implementation of JAX-WS.Most commonly used implementation of JAX-WS are:

  • Metro(Sun Microsystem)
  • Apache Axis
  • CXF

Here I am using Metro implementation to build the web service. Before being start learn following annotations.


This annotation is used to mark a endpoint implementation as implementing a web service.


This annotation is used to expose a method as a web service operation.


SOAP based web service can be of RPC based or Document based.In RPC binding body of SOAP packet represents port operations defined in the WSDL whereas in the document style binding body of SOAP packet can be of any arbitrary xml document which has a xml schema.RPC style is used when method calls and results uses simple type and Document style is used when complex type is needed for methods calls and results.


It is used to annotate the service parameters of the service method to define their name in the WSDL.


It is used to annotate the name of result of the web service method.

Soap Server Application

Create a web application for SOAP services and do the following.Define service interface and service implementation class.

package mypack;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(name = "Adder", targetNamespace = "")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Adder {
    @WebResult(partName = "return")
    @Action(input = "", output = "")
    public int add(
        @WebParam(name = "num1")
        int num1,
        @WebParam(name = "num2")
        int num2);

package mypack;
import javax.jws.WebService;
@WebService(endpointInterface = "mypack.Adder")
public class AdderImpl implements Adder{
	public int add(int a,int b) {
		return a+b;

Required Jars

Add following jars to WEB-INF/lib directory.

  • webservices-api.jar
  • webservices-rt.jar

Add following servlet and listener entry to web.xml which will generate WSDL and web service requests.


Now place sun-jaxws.xml to WEB-INF directory and add the service endpoints entries.

<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="">
  <endpoint implementation="mypack.AdderImpl" name="Adder" url-pattern="/*"/>

After running server and deploy the web application you are able to view the WSDL document by below url.

Client Application

Now we need to define client application to test the web services.Client application can be any programming languages like PHP,.Net or Java.Create java project and do the following steps.

  • Use wsimport tool to generate artifacts that will provide the way to communicate with server and call the service methods.
    Open command prompt and run the following command.
    c:\SoapRpc\src>wsimport -keep http://localhost:8080/SoapRpc?wsdl
  • It will generates the artifacts which contains the java files.
  • Add the jars mentioned above to the project.
  • Define to test the web service methods.
package mypack;
public class TestClient {
	public static void main(String[] args) {
		AdderImplService service = new AdderImplService();
		Adder port = service.getAdderImplPort();
		int a = port.add(10, 15);

Download SoAPRpc Server

Leave a Reply