- What You Will build
- What You Need
- Starting with Spring Initializr
- Add the Spring-WS dependency
- Create an XML Schema to Define the Domain
- Generate Domain Classes Based on an XML Schema
- Create Country Repository
- Create Country Service Endpoint
- Configure Web Service Beans
- Make the Application Executable
- Test the Application
- Summary
- See Also
This guide walks you through the process of creating a SOAP-based web service server with Spring.
You will build a server that exposes data from various European countries by using a WSDL-based SOAP web service.
Note
|
To simplify the example, you will use hardcoded data for the United Kingdom, Spain, and Poland. |
You can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
To manually initialize the project:
-
Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.
-
Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
-
Click Dependencies and select Spring Web and Spring Web Services.
-
Click Generate.
-
Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
Note
|
If your IDE has the Spring Initializr integration, you can complete this process from your IDE. |
Note
|
Both the pom.xml and build.gradle files need additional build information, which
you will add in the next step.
|
Note
|
You can also fork the project from Github and open it in your IDE or other editor. |
The project needs to include spring-ws-core
and wsdl4j
as dependencies in your build
file.
The following example shows the changes you need to make to the pom.xml
file if you use
Maven:
link:complete/pom.xml[role=include]
The following example shows the changes you need to make to the build.gradle
file if
you use Gradle:
link:complete/build.gradle[role=include]
The web service domain is defined in an XML schema file (XSD) that Spring-WS will automatically export as a WSDL.
Create an XSD file with operations to return a country’s name
, population
, capital
,
and currency
. The following listing (from src/main/resources/countries.xsd
) shows the
necessary XSD file:
link:complete/src/main/resources/countries.xsd[role=include]
The next step is to generate Java classes from the XSD file. The right approach is to do this automatically during build time by using a Maven or Gradle plugin.
The following listing shows the necessary plugin configuration for Maven:
link:complete/pom.xml[role=include]
Generated classes are placed in the target/generated-sources/jaxb/
directory.
To do the same with Gradle, you first need to configure JAXB in your build file, as the following listing shows:
link:complete/build.gradle[role=include]
Note
|
The build files have tag and end comments. These tags make it easier to extract
bits of it into this guide for a more detailed explanation. You do not need these comments
in your own build file.
|
The next step is to add the genJaxb
task, which Gradle uses to generate Java classes.
We need to configure gradle to find these generated Java classes in build/generated-sources/jaxb
and add genJaxb
as a dependency of compileJava
task.
The following listing shows the necessary addition:
link:complete/build.gradle[role=include]
Because 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.
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. The following
listing (from src/main/java/com/example/producingwebservice/CountryRepository.java
)
shows how to do so:
link:complete/src/main/java/com/example/producingwebservice/CountryRepository.java[role=include]
To create a service endpoint, you need only a POJO with a few Spring WS annotations to
handle the incoming SOAP requests. The following listing (from
src/main/java/com/example/producingwebservice/CountryEndpoint.java
) shows such a class:
link:complete/src/main/java/com/example/producingwebservice/CountryEndpoint.java[role=include]
The @Endpoint
annotation registers the class with Spring WS as a potential
candidate for processing incoming SOAP messages.
The @PayloadRoot
annotation is then used by Spring WS to pick the handler
method, based on the message’s namespace
and localPart
.
The @RequestPayload
annotation 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.
Note
|
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.
|
Create a new class with Spring WS-related beans configuration, as the following listing
(from src/main/java/com/example/producingwebservice/WebServiceConfig.java
) shows:
link:complete/src/main/java/com/example/producingwebservice/WebServiceConfig.java[role=include]
-
Spring WS uses a different servlet type for handling SOAP messages:
MessageDispatcherServlet
. It is important to inject and setApplicationContext
toMessageDispatcherServlet
. Without that, Spring WS will not automatically detect Spring beans. -
Naming this bean
messageDispatcherServlet
does not replace Spring Boot’s defaultDispatcherServlet
bean. -
DefaultMethodEndpointAdapter
configures the annotation-driven Spring WS programming model. This makes it possible to use the various annotations, such as@Endpoint
(mentioned earlier). -
DefaultWsdl11Definition
exposes a standard WSDL 1.1 by usingXsdSchema
Important
|
You need to specify bean names for
MessageDispatcherServlet and
DefaultWsdl11Definition . Bean names determine the URL under
which the web service and the generated WSDL file are 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.
Spring Boot creates an application class for you. In this case, it needs no further
modification. You can use it to run this application. The following listing (from
src/main/java/com/example/producingwebservice/ProducingWebServiceApplication.java
) shows
the application class:
link:complete/src/main/java/com/example/producingwebservice/ProducingWebServiceApplication.java[role=include]
Logging output is displayed. The service should be up and running within a few seconds.
Now that the application is running, you can test it. Create a file called request.xml
that contains the following SOAP request:
link:complete/request.xml[role=include]
The are a few options when it comes to testing the SOAP interface. You can use something similar to SoapUI or use command-line tools if you are on a *nix/Mac system. The following example uses curl from the command line:
# Use data from file
curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws
# Use inline XML data
curl <<-EOF -fsSL -H "content-type: text/xml" -d @- http://localhost:8080/ws \
> target/response.xml && xmllint --format target/response.xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:gs="http://spring.io/guides/gs-producing-web-service">
<soapenv:Header/>
<soapenv:Body>
<gs:getCountryRequest>
<gs:name>Spain</gs:name>
</gs:getCountryRequest>
</soapenv:Body>
</soapenv:Envelope>
EOF
As a result, you should see the following 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>
Note
|
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 -fsSL --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws > output.xml and xmllint --format output.xml
see the results formatted nicely.
|
The following guides may also be helpful: