Contract First SOAP Service with Spring and Maven

1. Introduction

In this tutorial, we will learn to implement a contract-first SOAP service application using JAX-WS, Spring and Maven. This is more of a design decision whether to use contract-first or code-first approach.

The most notable benefit of using applying contract-first approach in developing a SOAP based web service application, is that the contract can be shared with the consumers/clients immediately after the needed changes are done to it, so the client application and web service operation implementation can be done independently by different teams at the same time, thus saving a lot of time.

2. Implementation

Contract First Web Service

Something like above would be the scenario, where the client/consumer application will be interacting with our sample SOAP based JAX-WS web service through a service endpoint.


To start with the implementation, first create a Maven Project in Eclipse and make sure we proceed with the directory structure close to what has been shown below.

JAX-WS SOAP Project Structure

First is to look at our pom dependencies, which should look like –

pom.xml

Note the wsimport tool that we will be using, to generate the stub files later from the WSDL file.

Let’s now check out the web.xml file.

web.xml

Next we create a schema file for our web service. Name it as customerService.xsd, which will basically define the building blocks for the WSDL file or web service contract, we will be creating.

customerService.xsd

Create a valid WSDL file using the XML schema we just created.

customerService.wsdl

Next is to generate the stub files from the WSDL file. To do that, run the below command in your system’s command prompt.

Check out for the generated stub files in the /target directory.

JAX-WS Contract first service Stub files

Let’s now create the implementation of the generated service interface CustomerServicePortType in a class named CustomerServiceImpl.

CustomerServiceImpl.java

@WebService annotation must be applied to the endpoint interface implementation class to mark it as the web service endpoint. The @WebService annotation tells the server runtime environment to expose all the public methods of that class as web service methods.

We are all done with the application. Final check is if we can see the WSDL content displayed when the below endpoint URI is hit, whose location we have specified towards the bottom of the WSDL file.

http://localhost:8080/SOAPWebServiceExample/customer?wsdl

Below is what we see in the browser when we hit it.

WSDL file content displayed in browser

So yes, we did it, successfully.

3. Running the application

Setup the SOAP project in SOAP UI with the WSDL URI (http://localhost:8080/SOAPWebServiceExample/customer?wsdl).

Below is what we see when we actually hit the service in SOAP UI.

Hitting the service through SOAP

4. Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.