By Marone: July 2019 | last update: July 2020
Soap with Spring boot - Schema first
Goal
In the previous article we saw how to use Spring boot contract-first approach starting from a wsdl file. We will flow the contract-first with schema approach. Just keep in mind Spring boot is using Spring-WS under the hoodUsed technologies
JDK 1.8Maven 3.2
XSD First
Info! According to Spring WS documentation the Resquest/Response/Fault elements should be marked with
updateTutorial
updateTutorial
tutorial
suffix
. We are using the default ones:
updateTutorial
Request
updateTutorial
Response
tutorial
Fault
Maven dependencies
The
spring-boot-starter-web-services
dependency includes the needed dependencies for using Spring-WS
WSDL4J
The Web Services Description Language for Java Toolkit
allows the creation, representation, and manipulation of WSDL documents.
In Order to use schema elements in Endpoint class we need at first to generate Java classes from the xsd file The
jaxb2-maven-plugin
will be used to handle the code generation.
The Plugin looks in the path defined in
<source>
to find any XSD file
The
<outputDirectory>
is the location where the java classes will be generated
Generate code
The generated java classes are under/src
and looks like:
Configuration
Notes:
@EnableWs
: Provides spring web service configuration- In lines (19-22) we define the xml schema.
- We define DefaultWsdl11Definition with
@Bean(name = "tutorials")
as name, this name will be the name of the WSDL in the URLwsdl11Definition.setSchema(tutorialsSchema)
, use the schema we define abovewsdl11Definition.setPortTypeName("TutorialsPort")
, set the port type namewsdl11Definition.setLocationUri("/ws")
, The path in URL for the exposed WSDL
- MessageDispatcherServlet will be used to handle the http requests
- Setting *ApplicationContext* is required
- The
ServletRegistrationBean
maps all the incoming requests with URI/ws/*
to the servlet
- The url of the wsdl will look like: scheme://host:port/(servlet mapping uri)/(DefaultWsdl11Definition bean name).wsdl, which is equivalent to http://localhost:8090/ws/tutorials.wsdl
Implementing the Endpoint
After expsoing the wsdl now we need some implementation to handle incoming XML messages, in order to do that we need a class with one or more handling methods. In this tutorial we will implement only two methods Details:@Endpoint
, this class becomes web service Endpoint
The method annotated with
@PayloadRoot
becomes an Endpoint method. Depending on the attributes namespace and localPart Spring-WS will forward the incoming request to a appropriate method
@RequestPayload
and @ResponsePayload
are for mapping request and response values
Run the SOAP Web Service
Big picture
Test with soapUI
According toapplication.properties
the application is running on port 8090
The WSDL is now located at http://localhost:8090/ws/tutorials.wsdl and the endpoint URL is http://localhost:8090/ws