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.
- JDK 1.8 +
SOAP services can be developed with two methods
- Contract First : Define WSDL and Schema before writing any code.
- Contract Last: Auto-generate the WSDL and schemas from the java classes.
Spring-ws only supports the contract-first approach
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
Contract first approach requires us to define the schema. And then we’ll use Spring-ws auto-generate WSDL out of the schema.
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.
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.
- 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.
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.
$ mvn spring-boot:run
Now, you can see the WSDL at the following location
- Import the wsdl in SOAP-UI & soap-ui will auto generate request structure for the request.
- Send request to service
You can clone or download this project from Github