Producing a SOAP web service :: Learn how to create a SOAP-based web service with Spring.
Shell Batchfile Java


tags projects

This guide walks you through the process of creating a SOAP-based web service server with Spring.

What you’ll build

You will build a server that exposes data from various European countries using a WSDL-based SOAP web service.

To simplify the example, you will use hardcoded data for the United Kingdom, Spain and Poland.

Add Spring-WS dependency

The project you create needs to include spring-ws-core as a dependency in your build file as well as wsdl4j.

For maven:


For gradle:


Create an XML schema to define the domain

The web service domain is defined in an XML schema file (XSD) that Spring-WS will export automatically as a WSDL.

Create an XSD file with operations to return a country’s name, population, capital and currency:



Generate domain classes based on an XML schema

The next step is to generate Java classes from the XSD file. The right approach is do this automatically during build time using a maven or gradle plugin.

Plugin configuration for maven:


Generated classes are placed in target/generated-sources/jaxb/ directory.

To do the same with gradle, first you need to configure JAXB in your build file:

The build file above has tag and end comments. This is to make it easier to extract bits of it into this guide for more detailed explanation. These comments aren’t needed in your own build file.

Next step is to add task genJaxb used by gradle to generate Java classes:


As gradle does not have a JAXB plugin (yet), it involves an ant task, which makes it a bit more complex than in maven.

In both cases, the JAXB domain object generation process has been wired into the build tool’s lifecycle so there are no extra steps to run.

Create country repository

In order to provide data to the web service, create a country repository. In this guide you create a dummy country repository implementation with hardcoded data.


Create country service endpoint

To create a service endpoint, you only need a POJO with a few Spring WS annotations to handle the incoming SOAP requests.


@Endpoint registers the class with Spring WS as a potential candidate for processing incoming SOAP messages.

@PayloadRoot is then used by Spring WS to pick the handler method based on the message’s namespace and localPart.

@RequestPayload indicates that the incoming message will be mapped to the method’s request parameter.

The @ResponsePayload annotation makes Spring WS map the returned value to the response payload.

In all of these chunks of code, the io.spring.guides classes will report compile-time errors in your IDE unless you have run the task to generate the domain classes based on the WSDL.

Configure web service beans

Create a new class with Spring WS related beans configuration:


It’s important to notice that you need to specify bean names for MessageDispatcherServlet and DefaultWsdl11Definition. Bean names determine the URL under which web service and the generated WSDL file is available. In this case, the WSDL will be available under http://<host>:<port>/ws/countries.wsdl.

This configuration also uses the WSDL location servlet transformation servlet.setTransformWsdlLocations(true). If you visit http://localhost:8080/ws/countries.wsdl, the soap:address will have the proper address. If you instead visit the WSDL from the public facing IP address assigned to your machine, you will see that address instead.

Make the application executable

Although it is possible to package this service as a traditional WAR file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main() method. Along the way, you use Spring’s support for embedding the Tomcat servlet container as the HTTP runtime, instead of deploying to an external instance.



Logging output is displayed. The service should be up and running within a few seconds.

Test the application

Now that the application is running, you can test it. Create a file request.xml containing the following SOAP request:


The are a few options when it comes to testing the SOAP interface. You can use something like SoapUI or just use command line tools if you are on a *nix/Mac system as shown below.

$ curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws

As a result you should see this response:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="">
    <ns2:getCountryResponse xmlns:ns2="">
Odds are that the output will be a compact XML document instead of the nicely formatted one shown above. If you have xmllib2 installed on your system, you can curl <args above> > output.xml | xmllint --format output.xml see the results formatted nicely.


Congratulations! You’ve developed a SOAP-based service using Spring Web Services.