SOAP Web Service Example in Java
1. Overview
In this article, we will take a look at the SOAP Web Service examples.
2. SOAP Web Service Example
Using Apache Axis, SOAP web services are built. SOAP is an acronym for Simple Object Access Protocol. SOAP is used for developing web services that are based on XML based industry-standard protocol. SOAP security is based on WS Security. SOAP web services are platform and language independent.
2.1 Prerequisites
Java 8 is required on the Linux, windows or mac operating system. Eclipse Oxygen can be used for this example. Apache Tomcat 9.0 is used as a servlet container to deploy the examples.
2.2 Download
You can download Java 8 from the Oracle web site . Eclipse Oxygen can be downloaded from the eclipse web site. Apache Tomcat 9.0 can be downloaded from the apache website.
2.3 Setup
Below are the setup commands required for the Java Environment.
Setup
JAVA_HOME="/desktop/jdk1.8.0_73" export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH
2.4 IDE
2.4.1 Eclipse Oxygen Setup
The ‘eclipse-java-oxygen-2-macosx-cocoa-x86_64.tar’ can be downloaded from the eclipse website. The tar file is opened by double click. The tar file is unzipped by using the archive utility. After unzipping, you will find the eclipse icon in the folder. You can move the eclipse icon from the folder to applications by dragging the icon.
2.5 Launching IDE
2.5.1 Eclipse Java
Eclipse has features related to language support, customization, and extension. You can click on the eclipse icon to launch eclipse. The eclipse screen pops up as shown in the screenshot below:
You can select the workspace from the screen which pops up. The attached image shows how it can be selected.
You can see the eclipse workbench on the screen. The attached screenshot shows the Eclipse project screen.
Java Hello World
class prints the greetings. The screenshot below is added to show the class and execution on eclipse.
2.6 SOAP Web Service in Java
SOAP is based on Web Services Description Language (WSDL). First, we create the service. The code below shows the service Greetings
implementation.
Greetings Service
public class Greetings { public String getMessage(String message) { return "received message "+ message; } }
WSDL is created by using the eclipse menu. The screenshot below shows the menu navigation.
In the screen, after selecting Next you will be navigated to the selection of the Service implementation. The screenshot below shows the service implementation selection of the Greetings
Service.
WSDL is created for the Greetings
Service. WSDL created is shown below in the code.
Greetings Service
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://DefaultNamespace" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://DefaultNamespace" xmlns:intf="http://DefaultNamespace" 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://DefaultNamespace" xmlns="http://www.w3.org/2001/XMLSchema"> <element name="getMessage"> <complexType> <sequence> <element name="message" type="xsd:string"/> </sequence> </complexType> </element> <element name="getMessageResponse"> <complexType> <sequence> <element name="getMessageReturn" type="xsd:string"/> </sequence> </complexType> </element> </schema> </wsdl:types> <wsdl:message name="getMessageRequest"> <wsdl:part element="impl:getMessage" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="getMessageResponse"> <wsdl:part element="impl:getMessageResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="Greetings"> <wsdl:operation name="getMessage"> <wsdl:input message="impl:getMessageRequest" name="getMessageRequest"> </wsdl:input> <wsdl:output message="impl:getMessageResponse" name="getMessageResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="GreetingsSoapBinding" type="impl:Greetings"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getMessage"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="getMessageRequest"> <wsdlsoap:body use="literal"/> </wsdl:input> <wsdl:output name="getMessageResponse"> <wsdlsoap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="GreetingsService"> <wsdl:port binding="impl:GreetingsSoapBinding" name="Greetings"> <wsdlsoap:address location="http://localhost:8080/WebServiceSoap/services/Greetings"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
The Greetings
web service is tested using the eclipse web service explorer. The test is successful as shown in the screenshot below.
The input for the test is set as greetings. The output is shown from the screenshot below:
The output is as expected – received message greetings.
3. Download the Source Code
You can download the full source code of this example here: SOAP Web Service Example in Java