By Alx: June 2019

Goal

In this tutorial we want to show how to build a saop web serivce with spring boot. Spring boot uses Spring-WS, which allows only contract-first. Hence we need to start from a contract definition, either from a xml schema (xsd) or from WSDL. We will flow the contract-first with wsdl approach

Used technologies

JDK 1.8
Maven 3.2

WSDL First


The wsdl document contains the five standard elements: types, message, portType, binding and service.
The wsdl provides 3 operations: deleteTutorial, updateTutorial and getTutorials

pom xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.wstutorial.ws</groupId>
	<artifactId>soap-spring-boot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-services</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.jvnet.jaxb2.maven2</groupId>
				<artifactId>maven-jaxb2-plugin</artifactId>
				<version>0.13.2</version>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
					<schemaIncludes>
						<include>*.wsdl</include>
					</schemaIncludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

The only used dependency isspring-boot-starter-web-services, it includes the needed dependencies for using Spring-WS
Maven plugin maven-jaxb2-plugin for generating Java sources from WSDL

  • schemaDirectory: Directory where WSDL file can be found.
  • schemaIncludes : Here we can specify which file should be used. In our case a WSDL file
  • generateDirectory - Target directory for the generated code, In our case is unspecified, so the default generateDirectory will be used target/generated-sources/xjc
  • generatePackage is not set, consequently the package name will be derived from the wsdl. Exactly from targetNamespace

Let's generate code


wsdl to code

Implementing the endpoint

A simple implemenation with dummy data

Configuration

Notes:

  • @EnableWs: Provides spring web service configuration
  • We define DefaultWsdl11Definition with @Bean(name = "tutorialService"). tutorialService will be the name of the WSDL in the URL
    • wsdl11Definition.setWsdl, spicify the localtion of the wsdl
  • MessageDispatcherServlet will be used to handle the http requests
    • Setting *ApplicationContext* is required
    • The ServletRegistrationBean maps all the incoming requests with URI /wsdlfirst/* to the servlet
  • The url of the wsdl will be: http://localhost:8080/wsdlfirst/tutorialService.wsdl

Run the SOAP Web Service


Test with soapUI

soapUI wsdl

References