# How to create SOAP webservice using Spring-WS & Spring Boot

## Overview

In this tutorial, we’ll see how to create a SOAP webservice using Spring-WS and Spring Boot. This post will focus only on pure Java based approach using annotations.

1. JDK 1.8 +
2. Maven
3. IDE

## Approach

SOAP services can be developed with two methods

1. Contract First : Define WSDL and Schema before writing any code.
2. Contract Last: Auto-generate the WSDL and schemas from the java classes.

Spring-ws only supports the contract-first approach

## Project setup

You can clone this project from Github to kick start the project

Create a maven project and add the following dependencies in the pom.xml

## Schema Design

Contract first approach requires us to define the schema. And then we’ll use Spring-ws auto-generate WSDL out of the schema.

## Generate Java Classes

Now, we’ll jaxb2-maven-plugin to generate the java classes from the schema. Add the below plugin in your pom.xml

• schemaDirectory : location of the schema
• outputDirectory: where we want our java classes.
• clearOutputDir : making this true will delete the classes every time you compile the project

Now, we’ll generate the classes by issuing a following maven command.

$mvn clean install  Now you can see the auto-generated classes in you project folder. ## Setup Endpoint Now, we’ll setup an endpoint in our Java code to serve the request. Create a class and annotate with @Endpoint For the purpose of this tutorial, I added very simple code to return a success response. • @Endpoint: This indicates that this class is a web service endpoint • @PayloadRoot: This indicates that incoming soap request for this method will have defined local part and namespace. It will basically try to match the RootElement of your xml message. • @ResponsePayload: This indicates that method will return a payload. ## Configure Servlet Bean & WSDL Definition • Define the ServletRegistrationBean in configuration to register a servlet which will listen the incoming requests. • Define the configuration for WSDL Definition • portTypeName : Interface name • locationUri : URL to expose service • targetNamespace: Target name space for the WSDL elements • schema: Location of the schema • @Bean(name = “customer”) :Name of this bean will be used the wsdl name. ## Bootstrap as Spring Boot Application Add following plugin in pom.xml to make the jar spring boot compatible. Define the main method which will allow this application to run using Spring Boot.This class should be in the root package always for the component scan. ## Deploy the service $ mvn spring-boot:run


Now, you can see the WSDL at the following location
http://localhost:8080/ws/customer.wsdl

## Test the service

• Import the wsdl in SOAP-UI & soap-ui will auto generate request structure for the request.

• Send request to service