Home » Enterprise Java » ejb3 » Web Services » SOAP Web Service Example in Java

About Bhagvan Kommadi

Bhagvan Kommadi
Bhagvan Kommadi is the Founder of Architect Corner & has around 19 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript.

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:

SOAP Web Service Example - Eclipse Launch
Eclipse Launch

You can select the workspace from the screen which pops up. The attached image shows how it can be selected.

SOAP Web Service Example - Eclipse Workspace
Eclipse Workspace

You can see the eclipse workbench on the screen. The attached screenshot shows the Eclipse project screen.

SOAP Web Service Example - Eclipse Workbench
Eclipse Workbench

Java Hello World class prints the greetings. The screenshot below is added to show the class and execution on eclipse.

SOAP Web Service Example - Java Hello
Java Hello

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.

SOAP Web Service Example - Webservice Menu
Webservice Menu

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.

Webservice Bottom Up

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.

WebService response

The input for the test is set as greetings. The output is shown from the screenshot below:

Webservice output

The output is as expected – received message greetings.

3. Download the Source Code

Download
You can download the full source code of this example here: SOAP Web Service Example in Java

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments