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.

@WebService

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

@WebMethod

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

@SOAPBinding

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.

@WebParam

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

@WebResult

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.
Adder.java

package mypack;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;
@WebService(name = "Adder", targetNamespace = "http://startwithjava.com/services")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Adder {
    @WebMethod
    @WebResult(partName = "return")
    @Action(input = "http://startwithjava.com/services/Adder/addRequest", output = "http://startwithjava.com/services/Adder/addResponse")
    public int add(
        @WebParam(name = "num1")
        int num1,
        @WebParam(name = "num2")
        int num2);
}

AdderImpl.java

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.

 <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>s2</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>s2</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

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="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
  <endpoint implementation="mypack.AdderImpl" name="Adder" url-pattern="/*"/>
</endpoints>

After running server and deploy the web application you are able to view the WSDL document by below url.
http://localhost:8080/SoapRpc?wsdl

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.
    eg.
    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 Test.java 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);
		System.out.println("Sum:-"+a);
	}
}

Download SoAPRpc Server

Leave a Reply