Create SOAP Web Service in Java using Eclipse
In this tutorial, we will build a web service using SOAP.
Step 1 : Create a dynamic web project in eclipse/STS
Step 2 : Fill in the deails as below
Step 3 : Click Next. Don’t forget to click the check boxes as shown below
Step 4 : Click Finish. The dynamic web project should be created like below
Step 5 : Create a Model Class called Operand under com.sks package
package com.sks;
public class Operand {
private int num1;
private int num2;
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
@Override
public String toString() {
return "Operand [num1=" + num1 + ", num2=" + num2 + "]";
}
}
Step 6 : Create an interface CalculationService. It will have operations like sum, multiplication
package com.sks;
public interface CalculationService {
public int sum(Operand op);
public int multiplication(Operand op);
}
Step 7 : Create its implementation class CalculationServiceImpl. It implements CalculationService interface
package com.sks;
public class CalculationServiceImpl implements CalculationService {
@Override
public int sum(Operand op) {
return op.getNum1() + op.getNum2();
}
@Override
public int multiplication(Operand op) {
return op.getNum1() * op.getNum2();
}
}
Now, our dynamic web project is ready with some logic. Next step is to create a web service from it.
Step 8 : Create a new project in STS/eclipse and choose ‘Web Service’ under ‘Web Services’ option as shown below
Step 9 : Now, click on Next and verify the details on the next screen. Make sure Web Service Type is selected as Bottom Up Java Bean Web Service.
Also, select Service implementation as our class CalculationServiceImpl. Also drag the Configuration bar to the maximum on the left hand side. It should be upto ‘Test Service’.
Step 10 : On the next screen, we can select which all methods we want to expose as web service. For this example, we will choose both
Step 11 : Click on Next. And cick on Start server button. Once it is started, click on Next button.
Step 12 : Below screen will appear. Test facility is selected as Web Services Explorer. Click on the Launch button.
Step 12 : Web Services Explorer will open in a new window. It will look like below. Don’t do anything here and move to the next step.
Step 13 : On the Eclipse, click on the Next button in the below screen
Step 14 : The below screen will appear. You don’t have to do anything on this screen. Just click on finish.
Step 15 : Go back to Web Services Explorer screen and click on sum as highlighted
Step 16 : Give values in the num1 and num2 boxes and click Go button
Step 17 : The result 26 can be seen under Status
Step 18 : To check the soap generated request and response, click on the source as shown below
SOAP request
SOAP request for this method is as below
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://sks.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:sum>
<q0:op>
<q0:num1>12</q0:num1>
<q0:num2>14</q0:num2>
</q0:op>
</q0:sum>
</soapenv:Body>
</soapenv:Envelope>
SOAP response
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<sumResponse xmlns="http://sks.com">
<sumReturn>26</sumReturn>
</sumResponse>
</soapenv:Body>
</soapenv:Envelope>
WSDL File
The WSDL file can be seen under the webapp folder as shown below
Below is the content of wsdl file
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://sks.com" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://sks.com" xmlns:intf="http://sks.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://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="http://sks.com" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="sum">
<complexType>
<sequence>
<element name="op" type="impl:Operand"/>
</sequence>
</complexType>
</element>
<complexType name="Operand">
<sequence>
<element name="num1" type="xsd:int"/>
<element name="num2" type="xsd:int"/>
</sequence>
</complexType>
<element name="sumResponse">
<complexType>
<sequence>
<element name="sumReturn" type="xsd:int"/>
</sequence>
</complexType>
</element>
<element name="multiplication">
<complexType>
<sequence>
<element name="op" type="impl:Operand"/>
</sequence>
</complexType>
</element>
<element name="multiplicationResponse">
<complexType>
<sequence>
<element name="multiplicationReturn" type="xsd:int"/>
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<wsdl:message name="multiplicationRequest">
<wsdl:part element="impl:multiplication" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sumRequest">
<wsdl:part element="impl:sum" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sumResponse">
<wsdl:part element="impl:sumResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="multiplicationResponse">
<wsdl:part element="impl:multiplicationResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="CalculationServiceImpl">
<wsdl:operation name="sum">
<wsdl:input message="impl:sumRequest" name="sumRequest">
</wsdl:input>
<wsdl:output message="impl:sumResponse" name="sumResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="multiplication">
<wsdl:input message="impl:multiplicationRequest" name="multiplicationRequest">
</wsdl:input>
<wsdl:output message="impl:multiplicationResponse" name="multiplicationResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculationServiceImplSoapBinding" type="impl:CalculationServiceImpl">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sum">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sumRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sumResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="multiplication">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="multiplicationRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="multiplicationResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CalculationServiceImplService">
<wsdl:port binding="impl:CalculationServiceImplSoapBinding" name="CalculationServiceImpl">
<wsdlsoap:address location="http://localhost:8080/CalculationService/services/CalculationServiceImpl"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>