Show List

Create SOAP Web Service Using JAX WS

In this tutorial we are going to create a SOAP web service using Java API for XML Web Services (JAX-WS). JAX-WS is a Java specification for creating web services. It provides a set of APIs and tools for creating, deploying, and consuming web services. JAX-WS allows to create both SOAP and REST web services in Java. 

SOAP services can be created using any of the below approaches:
  • Contract First: Here the contract (WSDL file) is created first and Java classes are developed based on the contract.
  • Contract Last: Here Java classes are created first and then the WSDL file is created.
This example follows Contract last approach

We are using Eclipse IDE for this demo. Here is the structure of the project we are going to create:

Project Setup and Service Development

In the Eclipse IDE from the menu bar go to File > New > Project > Dynamic Web Project
Enter the project name select run time of you already have a server set up and click on finish. If you do not have the tomcat server set up on Eclipse you can follow the steps from here for the set up.

As we will be adding some maven dependency, convert the project to maven project by right click > Configure > Convert to Maven Project
In the generated pom file add jaxws-rt dependency as below

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>soapservice</groupId>
    <artifactId>soapservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.3.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
            </plugin>
        </plugins>
    </build>
</project>
Under src/main/java folder create soapws package and Calculator.java class as below:

Calculator.java
package soapws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

 
@WebService
@SOAPBinding(style = Style.RPC)
public class Calculator {
 
    @WebMethod
    public int add(int x, int y){
        return  x+y;
    }

    @WebMethod
    public int subtract(int x, int y){
        return  x-y;
    }

}
Under src > main > webapp > WEB-INF folder create web.xml and sun-jaxws.xml files with below code:

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>soapws</display-name>
  <listener>
    <listener-class>
      com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>JAXWSServlet</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>JAXWSServlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>
The <listener> element is used to specify a listener class that the web container should execute when the application is deployed. In this case, the listener class is com.sun.xml.ws.transport.http.servlet.WSServletContextListener, which is a listener class provided by the JAX-WS library that sets up the context for the web service servlets.

The <servlet> element is used to define a servlet that should be executed by the web container. In this case, the servlet-name is JAXWSServlet, and the servlet-class is com.sun.xml.ws.transport.http.servlet.WSServlet, which is a servlet provided by the JAX-WS library for handling SOAP web service requests.

The <servlet-mapping> element is used to map a URL pattern to a servlet. In this case, any requests to URLs that match the pattern "/ws/*" will be handled by the JAXWSServlet servlet.

The <display-name> element is used to specify the name of the web application that will be displayed in the web container administration console.

sun-jaxws.xml :
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
    xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
    <endpoint name="CalculatorWebService"
        implementation="soapws.Calculator" url-pattern="/ws/calculator" />

</endpoints>


This  file is used to define the web service endpoints and their configuration.

The <endpoint> element is used to define a web service endpoint. Each endpoint has a unique name, an implementation class, and a URL pattern.

In this case, the endpoint is named "CalculatorWebService", the implementation class is "soapws.Calculator" and the URL pattern is "/ws/calculator".

This endpoint configuration maps the "/ws/calculator" url pattern to a web service implementation class called "soapws.Calculator". This means that any request to URLs that match the pattern "/ws/calculator" will be handled by the class "soapws.Calculator" as a web service.

Running the SOAP Service

Right click on the project and run on server. Select the tomcat server you set up earlier.
When the application is deployed on the server, you can access the SOAP WSDL from below url   
http://localhost:8080/soapws/ws/calculator
http://localhost:8080/soapws/ws/calculator?wsdl
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.3.2 git-revision#3d0bba4. -->
<!-- Generated by JAX-WS RI (https://github.com/eclipse-ee4j/metro-jax-ws). RI's version is JAX-WS RI 2.3.2 git-revision#3d0bba4. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soapws/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://soapws/" name="CalculatorService">
<types/>
<message name="add">
<part name="arg0" type="xsd:int"/>
<part name="arg1" type="xsd:int"/>
</message>
<message name="addResponse">
<part name="return" type="xsd:int"/>
</message>
<message name="subtract">
<part name="arg0" type="xsd:int"/>
<part name="arg1" type="xsd:int"/>
</message>
<message name="subtractResponse">
<part name="return" type="xsd:int"/>
</message>
<portType name="Calculator">
<operation name="add" parameterOrder="arg0 arg1">
<input wsam:Action="http://soapws/Calculator/addRequest" message="tns:add"/>
<output wsam:Action="http://soapws/Calculator/addResponse" message="tns:addResponse"/>
</operation>
<operation name="subtract" parameterOrder="arg0 arg1">
<input wsam:Action="http://soapws/Calculator/subtractRequest" message="tns:subtract"/>
<output wsam:Action="http://soapws/Calculator/subtractResponse" message="tns:subtractResponse"/>
</operation>
</portType>
<binding name="CalculatorPortBinding" type="tns:Calculator">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="add">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://soapws/"/>
</input>
<output>
<soap:body use="literal" namespace="http://soapws/"/>
</output>
</operation>
<operation name="subtract">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://soapws/"/>
</input>
<output>
<soap:body use="literal" namespace="http://soapws/"/>
</output>
</operation>
</binding>
<service name="CalculatorService">
<port name="CalculatorPort" binding="tns:CalculatorPortBinding">
<soap:address location="http://localhost:8080/soapws/ws/calculator"/>
</port>
</service>
</definitions>

Testing the SOAP Service

We are going to use SOAP UI to test the webservice. Open the application and select New SOAP project from the file menu
Enter the WSDL URL in the "Initial WSDL" field and click "OK"
Click on the request link (1) to view the request fields and enter the value for parameters (2). Click on the play button (3) to send the request.
Response will be displayed in the results pane
Same steps can be followed to test the subtract operation
Source Code:

https://github.com/it-code-lab/SOAP-JAX-WS

    Leave a Comment


  • captcha text