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.



import_contacts

You may also like:

Related Posts

  • 11 January

    Spring boot JDBC example

    Table of ContentsTools usedProject StructureCreate new Spring boot projectMaven configurationConfigure datasource in Application.propertiesCreate database tableCreate model classCreate Repository interface and implementationCreate service interface and implementationCreate Application classRun the applicationVerify database outputGithub Source code In this post, we will see Spring boot JDBC example. As we already know Spring boot does lot of auto configurations which […]

  • 02 January

    Spring boot devtools tutorial

    Table of ContentsOverviewJRebelIntroduction to Spring Boot DevToolsAdding Devtools to your projectSpring Boot DevTools FeaturesProperty DefaultsLoggingCachingAutomatic RestartLive ReloadGlobal SettingsRemote ApplicationsFile System WatchersConclusion 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 […]

  • 26 December

    Spring boot H2 Database example

    Table of ContentsWhat is H2 database?Tools usedGithub Source code:Project StructureCreate new Spring boot projectMaven configurationH2 database configurationOverride H2 databse defaultsModelRepositoryServiceControllerSpring boot main fileRun the applicationTest the applicationCreate movie objectGet all movie objectsDelete movie id 3 In this post, we are going to see how to create Spring boot application integrating with H2 database. What is […]

  • 22 December

    Spring Boot CrudRepository

    Table of ContentsTools usedProject Structure:Wiring up MongoDBThe Repository ClassThe Save methodThe User Entity ClassBringing the UserDao and CrudRepository togetherServiceControllerApplication classRunning the code sampleConclusionSource code 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 […]

  • 10 April

    Difference between Spring and Spring boot

    Table of ContentsSpring frameworkSpring bootAdvantages of Spring BootSpring vs 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 […]

  • 08 August

    Spring boot profiles example

    Table of ContentsNeed for profilesProfiles for environment specific propertiesSetting active profileSpring boot @Profile annotationProject StructureSource code 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. For […]

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.