Soap Web Service – Introduction

Last modified on September 7th, 2014 by Joe.

This article is to help us understand what is a soap web service. Through this tutorial, we will learn to create a simple hello world web service and a web service client to consume it, of course all in java.

All my beginner friends, go choose your weapon. I repeat, programmer without an IDE is zorro without a sword! I have chosen Eclipse as my IDE, there are numerous free IDE available. You may choose Eclipse or Netbeans. After choosing the IDE your next priority is to learn the IDE (Eclipse) shortcuts.



For this article to create a hello world soap webservice, I have chosen Netbeans as the IDE. Netbeans provides better out of the box support for web service development.

Soap webservice is cumbersome to work with in comparison to RESTful web services. If the soap web service development is going to happen using notepad then that is the first dumbest big mistake in the development plan.

What is a web service?

W3C defintion for a web service is, “a software system designed to support interoperable machine-to-machine interaction over a network”.

Web service is an interface for your software. We may have web user interface or a thick client (desktop) user interface for our software. Imagine web service as another similar interface. This interface is not for humans but for softwares.

Web/thick client directly serves end user as an interface to interact with the software. Web service serves as an interface to software developers. Using web service as an API, developers can build external systems that will interact with the software.

There are two major categories of web services

SOAP Web Service RESTful Web Service

Soap Web Service

Simple Object Access Protocol (SOAP) is a standard protocol specification for message exchange based on XML. Communication between the web service and client happens using XML messages. SOAP defines the rules for communication like what are all the tags that should be used in XML and their meaning.

RESTful Web Service

RESTful web service uses architectures that use HTTP or similar protocols by restricting the interface to use standard operations like GET, POST, PUT, DELETE for HTTP. Based on my experience RESTful is easier to develop. I know this statement will invite wrath of SOAP lovers.

WSDL

One major component of a web service is Web Services Description Language (WSDL). It is an xml file that describes the web service technically in a machine readable format. That is, using this WSDL file we can understand things like,

Port / Endpoint – URL of the web service (using which we should access it)

Input message format

Output message format

Security protocol that needs to be followed (like https)

Which protocol the web service uses

I just want this article to be a beginning point for your web service exploration. Do not stop with this, explore more and more and you will love this service oriented architecture. Theory is too dull isn’t it? Lets move to fun part the coding.

Hello World SOAP Web Service

Download the latest Netbeans IDE. Go! I will wait… I am using NetBeans IDE 7.1 for this tutorial. You can download the complete source – SOAPHelloWorld of this article.

A Basic Web Application

File – New Project (Ctrl + Shift + n)

In ‘Categories:’ choose “Java Web”, in ‘Projects:’ choose “Web Application” and click Next.



In ‘Project Name:’ type “SOAPHelloWorld” and click Next.

By default ‘Server:’ is “GlassFish Server 3.1.1” and ‘Java EE Version:’ is “Java EE 6 Web” and click Finish.

Now you have created a basic web application. By default NetBeans creates web.xml and a welcome page (Hello World) jsp page.

Just to ensure our basic setup is good, right click on the project name and click ‘Run’.

(If you have firewall running, it may popup to allow access – just allow access)

We should get “Hello World!” message in the browser. This is just a basic web application printing hello world. Now we want to do the same thing using a web service.

Towards Web Service

Right click on project name, select ‘New’ and then select ‘Web Service’

Type ‘Web Service Name:’ as “HelloWorld”, then type ‘Package:’ as “com.javapapers.webservice” and click Finish.

Thats it! Web service development is done! Oh my God this is too much. Is that so easy?? Till now, we have heard a lot about web services but we never thought of it to be so easy! Wait wait, before we relax..

NetBeans has created this SOAP based hello world web service. Lets understand what it has done in the background and verify if it is working.

Service Code

Following is the java code that serves as the web service and it is generated by Netbeans.

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.javapapers.webservice; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * * @author USER */ @WebService(serviceName = "HelloWorld") public class HelloWorld { /** * This is a sample web service operation */ @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + " !"; } }

hello is the operation that returns “Hello” + the parameter sent as argument.

WSDL of Web Service

Now right click the project and click run. Then type the following url in the browser

http://localhost:8080/SOAPHelloWorld/HelloWorld?wsdl

it will return the wsdl of the soap web service that we have created now. This wsdl contains,

xml definition

XSD schema import

Two message tags hello and helloResponse (request and response)

Porttype definition including operation hello

Binding describes, we use soap, then operation, input and output.

Service definition, name, port and address for invocation.

Web Service Client

Now lets invoke this webservice and see how it works.

Open the index.jsp that was generated by NetBeans

Remove the “Hello World!” string. We are going to access the now created soap web service in the place and print whatever the web service returns.

Right click on the project and choose ‘New’ then ‘Web Service Client…’

In the New Web Service Client dialog select ‘WSDL URL:’ and type the wsdl url in the text box ‘http://localhost:8080/SOAPHelloWorld/HelloWorld?wsdl’. Then in package, type com.javapapers.webserviceclient and click Finish.

Invoke the Web Service

Now the web service client is ready. We need to invoke the hello world soap web service using the web service client we have created now.

In that place right click choose “Web Service Client Resources” then select “Call Web Service Operation…”

From that dialog select “hello” and clik ok. This is the operation we are invoking of that web service.

Following is the code snippet inserted by Netbeans IDE to access the web service.

In the above code snippet, for vaiable ‘name’ assign a string value you prefer like,

java.lang.String name = "javapapers";

Right click the project and select ‘run’ and you get “Result = Hello javapapers ! ”

You can download the complete source – SOAPHelloWorld of this article.