Publish a SOAP Web Service With Spring Boot
Design document-driven, contract-first SOAP web services with Spring Boot and Spring Web Services.
SOAP Web Services (Spring-WS) is an offering from Spring Framework that facilitates creating document-driven, contract-first SOAP web services. One of the main reasons to use Spring-WS is that it has built-in integration with Spring Framework and the ability to leverage major Spring features such as dependency injection. Also, the architecture of Spring-WS resembles that of Spring-MVC and this makes the learning of Spring-WS much easier.
In this article, we will develop a contract-first soap web service with Spring boot. In the development process, we will highlight the core Spring-WS features.
What are we Building?
We are developing a SOAP web service that let users perform basic arithmetic calculation such as addition, subtraction, multiplication, and division. Note that Spring supports contract-first web service development. In the contract-first design approach, the service definitions are designed before the actual coding is done. In the next section, let us define the WSDL of our service.
In this section, let us create our WSDL file. In the WSDL file, we have the following elements:
- Schema definition
- Message definition
- PortType definition
- Binding definition
- Service definition
Creating a Spring Boot Project
Let us now create a new Spring Boot project. We only need spring-boot-starter-web-services dependency. Following is the final pom.xml file:
Generating Sources from the WSDL
We have added maven-jaxb2-plugin in our application to generate the POJO classes from the schema definition. In our WSDL, we have defined input and outputs for our calculator operations namely, AdditionInput, SubtractionInput, MultiplicationInput, DivisionInput, and output. All the input types accept two integers and output returns one integer.
Run the following command to generate the sources:
Now that our POJOs are ready, let us create the endpoints for our calculator. We will expose four endpoints for the operations. We will use Spring-WS’s PayloadRoot to map the endpoints. The following is the endpoint definitions.
Note that this class is annotated with @Endpoint annotation. This tells Spring that this class is a SOAP endpoint. We then annotate the method with PayloadRoot. We can relate this with Spring REST’s RequestMapping annotation. With PayloadRoot we specify the namespace and the type of the payload. We also annotate this method with ResponsePayload to indicate that this method returns a value that should be bound to getPayloadSource() of WebServiceMessage.
Defining MessageDispatcherServlet and WsdlDefinition
Let us now define the Spring Boot configuration. In this file, we define the MessageDispatcher servlet bean and WSDL definition. Note that this class is annotated with Spring’s @EnableWs. This is to provide necessary web service configuration support.
MessageDispatcherServlet is used to dispatch web service messages. This is similar to the Spring MVC’s DispatcherServlet.
We also define Wsdl11Definition to include our WSDL definition.
Test the Application
We will use Postman to test our SOAP endpoints. We can use other clients such as SOAPUI also to do this. Configure the application to start in 9091 port by setting up server.port property in application.properties file. Start the application.
In Postman, set the accept the Content-Type header to text/xml as SOAP messages are of XML content type.
Testing Addition Service
Let us now test our additional endpoint. Following is the SOAP request message. Note that the SOAP body takes a payload of AdditionInput type.
We receive the following response for the above request. The response is of type output. We have defined this in our schema definition.
Spring-WS has made SOAP web service development much easier by proving certain standards and in build integration with core Spring Framework. It also incorporates best practices and many out of the box support which we otherwise need to do. Complete source code for this project could be found in the following GitHub repository.