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

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

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.



import_contacts

You may also like:

Related Posts

  • 11 January

    Spring boot JDBC example

    In this post, we will see Spring boot JDBC example. As we already know Spring boot does lot of auto configurations which help us to avoid a lot of boilerplate code. In the case of JDBC, spring boot provides auto-configuration such as creating DataSource and JDBCTemplate objects automatically based on application.properties. So you just need […]

  • 02 January

    Spring boot devtools tutorial

    In this post, we’ll be looking up what does spring has to offer for developer experience. Overview Using IDEs like Eclipse, Spring Tool Suite, Intellij Idea a typical development setup includes an application code base that gets deployed on a Web Application Server like Wildfly or Tomcat or even Glassfish. The speed of development is […]

  • 26 December

    Spring boot H2 Database example

    In this post, we are going to see how to create Spring boot application integrating with H2 database. What is H2 database? H2 is open source database. It is very fast and smaller in size. It is in-memory database and keeps all data in memory. If you start and stop the application, all the data […]

  • 22 December

    Spring Boot CrudRepository

    In this post, we’ll be looking up how to create and use Spring Boot CrudRepository. SpringBoot CrudRepository provides sophisticated CRUD functionality for the type of entity you want to be managed.This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this […]

  • 10 April

    Difference between Spring and Spring boot

    In this post, we will see difference between Spring and Spring boot. If you have followed spring project over time, you might have noticed it has become increasingly complex. If you want to create a spring application, you still need to put a lot of efforts. Spring boot is introduced to save time to bootstrap […]

  • 08 August

    Spring boot profiles example

    In this tutorial, we will see about Spring boot profiles. Need for profiles Application development is complex process. You might have different environment for application development. Dev SIT QA Prod We require different configuration based on the environment. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech […]

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 *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.