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.|
What you’ll need
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.
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
To do the same with gradle, first you need to configure JAXB in your build file:
The build file above has
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
@ResponsePayload annotation makes Spring WS map the returned value to the response payload.
In all of these chunks of code, the
Configure web service beans
Create a new class with Spring WS related beans configuration:
Spring WS uses a different servlet type for handling SOAP messages:
MessageDispatcherServlet. It is important to inject and set
MessageDispatcherServlet. Without that, Spring WS will not detect Spring beans automatically.
By naming this bean
messageDispatcherServlet, it does not replace Spring Boot’s default
It’s important to notice that you need to specify bean names for
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
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="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <ns2:getCountryResponse xmlns:ns2="http://spring.io/guides/gs-producing-web-service"> <ns2:country> <ns2:name>Spain</ns2:name> <ns2:population>46704314</ns2:population> <ns2:capital>Madrid</ns2:capital> <ns2:currency>EUR</ns2:currency> </ns2:country> </ns2:getCountryResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
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
Congratulations! You’ve developed a SOAP-based service using Spring Web Services.