Spring Boot SOAP Web service Example

In this post, we will see how to create soap web services with Spring boot.

We will create contract first soap web service with Spring boot. We will focus on how to define configurations for soap web services.


Tools used

    • JDK 1.8, Eclipse, Maven
    • Spring-boot – Underlying application framework
    • wsdl4j – for publishing WSDL for our Service
    • JAXB maven plugin – for code generation
    • SOAP-UI – for testing our soap web service

Project structure

Spring boot SOAP example

Let’s create a simple spring boot application.

Step 1: Go to "https://start.spring.io/" and create spring boot projects as per below screenshot.

Spring boot soap example

Step 2: Import the maven project in eclipse.


Add wsdl4j dependency

Step 3: Add wsdl4j dependency to the POM.


XSD file to define domain

step 4: Create a xsd file named "book.xsd" in resource folder. As we are creating contract first soap web services, we need to define XML schema file (XSD) that Spring-WS will export automatically as a WSDL.

We are creating book.xsd which will return book with its id, title and number of pages.


Generate domain classes based on XSD

Step 5: We will generate domain classes based on XSD now. We will use JAXB maven plugin to generate domain classes based on XSD.

Plugin configuration for maven

So when you run maven build, these domain classes will be generated.


Create Book repository with dummy data

Step 6: Create a BookRepository.java.This repository will provide dummy data to web service.


Define Book service endpoint

step 7: To create Book service endpoint, we just need to annotate a POJO with Spring WS annotation to take care of SOAP request.

Let’s see more details about above annotations.
@Endpoint: This annotation is used to register the class with Spring WS for processing incoming SOAP request.
@PayloadRoot: This annotation helps Spring WS to pick handler method based on message’s namespace and localPart.
@ResponsePayload: This annotation indicates that incoming message will be mapped to method’s request parameter.
@ResponsePayload: This annotation is used to Spring WS map the return value to the response payload.


Define web service configuration

step 8: Create a new class named WebServiceConfiguration.java which will contain Spring Web services related beans configurations.

Spring Ws usees MessageDispatcherServlet to handle SOAP message. It is important to set ApplicationContext, otherwise Spring WS will not be able to detect Spring bean automatically.
DefaultWsdl11Definition exposes a Standard WSDL using XsdSchema.

Please note that bean name for defaultWsdl11Definition defines URL for WSDL, so WSDL URL with above configuration will be
http://localhost:8080/ws/booksWsdl.wsdl.

step 9: Create the main java class named “SpringBootSoapExampleApplication.java” which will have main method.


Run the applicaton

Step 10: Run the application
When you run above application, you will get below output:



2018-09-16 20:30:37.597 INFO 24898 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-16 20:30:38.157 INFO 24898 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-09-16 20:30:38.348 INFO 24898 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ”
2018-09-16 20:30:38.355 INFO 24898 — [ main] o.a.j.SpringBootSoapExampleApplication : Started SpringBootSoapExampleApplication in 11.127 seconds (JVM running for 14.293)

Test WSDL URL

Step 11: Check WSDL URL.

URL : http://localhost:8080/ws/booksWsdl.wsdl
Hit the above URL and you will get below WSDL file.


Test Spring soap web service

Step 11: Test application on SOAP UI
Create a project on SOAP UI using above WSDL file and test the application.
SOAP request

SOAP response

Here is the screenshot from SOAP UI.

SOAP web service testing


Source code

That’s all about Spring Boot SOAP Webservice Example.


Was this post helpful?

Comments

  1. Hi … thanks for your tutorial. plz check this both the places written @ResponsePayload
    instead of one @RequestPayload.

    @ResponsePayload: This annotation indicates that incoming message will be mapped to method’s request parameter.
    @ResponsePayload: This annotation is used to Spring WS map the return value to the response payload.

Leave a Reply

Your email address will not be published. Required fields are marked *