By Marone: July 2019 | last update: December 2019

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 hood

Used technologies

JDK 1.8
Maven 3.2

XSD First


Info! According to Spring WS documentation the Resquest/Response/Fault elements should be marked with suffix. We are using the default ones:
updateTutorialRequest
updateTutorialResponse
tutorialFault

pom.xml


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

mvn clean generate-resources
The generated java classes are under /src and looks like:

xml schema

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 URL
    • wsdl11Definition.setSchema(tutorialsSchema), use the schema we define above
    • wsdl11Definition.setPortTypeName("TutorialsPort"), set the port type name
    • wsdl11Definition.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

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

package com.wstutorial.ws;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SchemaFirstApp {
    public static void main(String[] args) {
        SpringApplication.run(SchemaFirstApp.class, args);
    }
}

Big picture

big picture

Test with soapUI

According to application.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 soapUI

References