Spring Boot Soap WebServices Example | Soap Web Services Spring Boot

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to see how to implement Spring Boot Soap Webservices with a simple use case.
SOAP stands for Simple Object Access Protocol. SOAP is an XML based protocol for accessing web services over HTTP protocol.
There are two ways to develop SOAP-based web services: Contract First Approach and Contract Last Approach. You can see more about SOAP here.
In this example, I am going to implement a Contract First Approach.
That means, first we need to create a WSDL file, from which we have to generate java binding classes. Let’s see how it works.
Use Case: Creating a simple Item web service which provides the item information based on item id.
Spring Boot Starter Web Services
WSDL4J
Jaxb2
Maven
Java 8
A typical Spring boot application structure. Some of the packages will be added when we build the application; I will make you alert when it comes.
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
  </dependency>
  <dependency>
    <groupId>wsdl4j</groupId>
    <artifactId>wsdl4j</artifactId>
  </dependency>
</dependencies>
Add the below maven plugin to generate Java artefacts.
<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <id>xjc</id>
            <goals>
              <goal>xjc</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
          <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
          <clearOutputDir>false</clearOutputDir>
        </configuration>
      </plugin>
<?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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.onlinetutorialspoint</groupId>
  <artifactId>Spring-Boot-Soap-Web-Services</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring-Boot-Soap-Web-Services</name>
  <description>Spring Boot Soap Web Services Example</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <dependency>
      <groupId>wsdl4j</groupId>
      <artifactId>wsdl4j</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <id>xjc</id>
            <goals>
              <goal>xjc</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
          <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
          <clearOutputDir>false</clearOutputDir>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Creating a .xsd file under resources folder for XML request and response.
It contains a simple request (ItemRequest) and response (ItemResponse) schema definitions.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://onlinetutorialspoint.com/generated"
           xmlns:tns="http://onlinetutorialspoint.com/generated" elementFormDefault="qualified">
    <xs:element name="ItemRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name= "id" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="ItemResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="id" type="xs:int"/>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="category" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

After these steps build the application so that we could see the generated package under the com/onlinetutorialspoint package. It contains all java binding classes like below.
Below are the generated java resources.
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2019.07.20 at 12:37:49 AM IST 
//


package com.onlinetutorialspoint.generated;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id"
})
@XmlRootElement(name = "ItemRequest")
public class ItemRequest {

    protected int id;

    /**
     * Gets the value of the id property.
     * 
     */
    public int getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     */
    public void setId(int value) {
        this.id = value;
    }

}

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2019.07.20 at 12:37:49 AM IST 
//


package com.onlinetutorialspoint.generated;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *         &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="category" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "name",
    "category"
})
@XmlRootElement(name = "ItemResponse")
public class ItemResponse {

    protected int id;
    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected String category;

    /**
     * Gets the value of the id property.
     * 
     */
    public int getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     */
    public void setId(int value) {
        this.id = value;
    }

    /**
     * Gets the value of the name property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setName(String value) {
        this.name = value;
    }

    /**
     * Gets the value of the category property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getCategory() {
        return category;
    }

    /**
     * Sets the value of the category property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCategory(String value) {
        this.category = value;
    }

}

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2019.07.20 at 12:37:49 AM IST 
//


package com.onlinetutorialspoint.generated;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the com.onlinetutorialspoint.generated package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.onlinetutorialspoint.generated
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link ItemResponse }
     * 
     */
    public ItemResponse createItemResponse() {
        return new ItemResponse();
    }

    /**
     * Create an instance of {@link ItemRequest }
     * 
     */
    public ItemRequest createItemRequest() {
        return new ItemRequest();
    }

}

To make the spring boot application as a soap web service, we have to initialise certain beans while boot-strap the application.
package com.onlinetutorialspoint.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
import sun.java2d.pipe.SpanShapeRenderer;

@Configuration
@EnableWs
public class SoapConfig {
    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServletServlet(ApplicationContext applicationContext){
        MessageDispatcherServlet dispatcherServlet = new MessageDispatcherServlet();
        dispatcherServlet.setApplicationContext(applicationContext);
        dispatcherServlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<MessageDispatcherServlet>(dispatcherServlet,"/ws/*");
    }
    @Bean(name="item")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema xsdSchema){
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("Item");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://onlinetutorialspoint.com/generated");
        wsdl11Definition.setSchema(xsdSchema);
        return wsdl11Definition;
    }
    @Bean
    public XsdSchema xsdSchema(){
        return new SimpleXsdSchema(new ClassPathResource("item-details.xsd"));
    }
}

MessageDispatchServlet: This Servlet for simplified dispatching of Web service messages. We have to inject Spring ApplicationContext to this servlet to let it know about other beans.
ServletRegistrationBean: To register a Servlet (Registering MessageDispatcherServlet).
DefaultWsdl11Definition: Its a convenient implementation for DefaultWsdl11Definition that creates SOAP binding based on xsdSchema (item-details.xsd).  The bean name provides here; is assigned the name of the WSDL.
Creating an Item endpoint.
package com.onlinetutorialspoint.endpoint;

import com.onlinetutorialspoint.generated.ItemRequest;
import com.onlinetutorialspoint.generated.ItemResponse;
import com.onlinetutorialspoint.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class ItemEndPoint {
    private static final String NAME_SPACE = "http://onlinetutorialspoint.com/generated";
    @Autowired
    ItemService itemService;

    @PayloadRoot(namespace = NAME_SPACE, localPart = "ItemRequest")
    @ResponsePayload
    public ItemResponse getItems(@RequestPayload ItemRequest itemRequest){
        return itemService.getItem(itemRequest);
    }
}

@Endpoint annotation indicates that an annotated class is an endpoint. It registers the class as a Spring WS incoming soap message.
@PayloadRoot annotation marks an endpoint method as the handler for an incoming request.
@ResponsePayload annotation indicates that a method return value should be bound to the response payload.
@RequestPayload annotation indicates that a method parameter should be bound to the request payload.
It is a simple service class where our actual business logic goes.
package com.onlinetutorialspoint.service;

import com.onlinetutorialspoint.generated.ItemRequest;
import com.onlinetutorialspoint.generated.ItemResponse;
import org.springframework.stereotype.Service;

@Service
public class ItemService {

    public ItemResponse getItem(ItemRequest itemRequest){
        ItemResponse itemResponse = new ItemResponse();
        itemResponse.setId(itemRequest.getId());
        itemResponse.setCategory("Sample Category_"+itemRequest.getId());
        itemResponse.setName("Sample ItemName_"+itemRequest.getId());
        return itemResponse;
    }
}

package com.onlinetutorialspoint;

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

@SpringBootApplication
public class SpringBootSoapWebServicesApplication {

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

}
$mvn clean install
D:\work\Spring-Boot-Soap-Web-Services>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -------< com.onlinetutorialspoint:Spring-Boot-Soap-Web-Services >-------
[INFO] Building Spring-Boot-Soap-Web-Services 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ Spring-Boot-Soap-Web-Services ---
[INFO] Deleting D:\work\Spring-Boot-Soap-Web-Services\target
[INFO]
[INFO] --- jaxb2-maven-plugin:1.6:xjc (xjc) @ Spring-Boot-Soap-Web-Services ---
[INFO] Generating source...
[INFO] parsing a schema...
[INFO] compiling a schema...
[INFO] com\onlinetutorialspoint\generated\ItemRequest.java
[INFO] com\onlinetutorialspoint\generated\ItemResponse.java
[INFO] com\onlinetutorialspoint\generated\ObjectFactory.java
[INFO] com\onlinetutorialspoint\generated\package-info.java
....
....
$mvn spring-boot:run
[INFO] --- jaxb2-maven-plugin:1.6:xjc (xjc) @ Spring-Boot-Soap-Web-Services ---
[INFO] No changes detected in schema or binding files - skipping source generation.
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ Spring-Boot-Soap-Web-Services ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ Spring-Boot-Soap-Web-Services ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.1.5.RELEASE:run (default-cli) < test-compile @ Spring-Boot-Soap-Web-Services <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.5.RELEASE:run (default-cli) @ Spring-Boot-Soap-Web-Services ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.5.RELEASE)

2019-07-20 01:06:54.156  INFO 18868 --- [           main] c.o.SpringBootSoapWebServicesApplication : Starting SpringBootSoapWebServicesApplication on DESKTOP-RN4SMHT with PID 18868 (D:\w
ork\Spring-Boot-Soap-Web-Services\target\classes started by Lenovo in D:\work\Spring-Boot-Soap-Web-Services)
2019-07-20 01:06:54.177  INFO 18868 --- [           main] c.o.SpringBootSoapWebServicesApplication : No active profile set, falling back to default profiles: default
2019-07-20 01:06:56.361  INFO 18868 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [or
g.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$5e85ff3d] is not eligible for getting processed by 
...
...
On the above screen, you can see the soap request. By providing the item id, you can get the corresponding response.
SOAP protocol
Happy Learning 🙂

Spring Boot How to change the Tomcat to Jetty Server
Simple Spring Boot Example
Spring Boot SOAP Consumer Example
Spring Boot Apache ActiveMq In Memory Example
Spring Boot Kafka Producer Example
Spring Boot Basic Authentication Example
Spring Boot Environment Properties reading based on activeprofile
How to enable Swagger in Spring Boot Application
How To Change Spring Boot Context Path
How to change Spring Boot Tomcat Port Number
How to Create own Spring Boot Error Page
Spring Boot MongoDB + Spring Data Example
Spring Boot MVC Example Tutorials
Spring Boot JPA Integration Example
Spring Boot Multiple Data Sources Example

Spring Boot How to change the Tomcat to Jetty Server
Simple Spring Boot Example
Spring Boot SOAP Consumer Example
Spring Boot Apache ActiveMq In Memory Example
Spring Boot Kafka Producer Example
Spring Boot Basic Authentication Example
Spring Boot Environment Properties reading based on activeprofile
How to enable Swagger in Spring Boot Application
How To Change Spring Boot Context Path
How to change Spring Boot Tomcat Port Number
How to Create own Spring Boot Error Page
Spring Boot MongoDB + Spring Data Example
Spring Boot MVC Example Tutorials
Spring Boot JPA Integration Example
Spring Boot Multiple Data Sources Example
 


Δ
 Spring Boot – Hello World
 Spring Boot – MVC Example
 Spring Boot- Change Context Path
 Spring Boot – Change Tomcat Port Number
 Spring Boot – Change Tomcat to Jetty Server
 Spring Boot – Tomcat session timeout
 Spring Boot – Enable Random Port
 Spring Boot – Properties File
 Spring Boot – Beans Lazy Loading
 Spring Boot – Set Favicon image
 Spring Boot – Set Custom Banner
 Spring Boot – Set Application TimeZone
 Spring Boot – Send Mail
 Spring Boot – FileUpload Ajax
 Spring Boot – Actuator
 Spring Boot – Actuator Database Health Check
 Spring Boot – Swagger
 Spring Boot – Enable CORS
 Spring Boot – External Apache ActiveMQ Setup
 Spring Boot – Inmemory Apache ActiveMq
 Spring Boot – Scheduler Job
 Spring Boot – Exception Handling
 Spring Boot – Hibernate CRUD
 Spring Boot – JPA Integration CRUD
 Spring Boot – JPA DataRest CRUD
 Spring Boot – JdbcTemplate CRUD
 Spring Boot – Multiple Data Sources Config
 Spring Boot – JNDI Configuration
 Spring Boot – H2 Database CRUD
 Spring Boot – MongoDB CRUD
 Spring Boot – Redis Data CRUD
 Spring Boot – MVC Login Form Validation
 Spring Boot – Custom Error Pages
 Spring Boot – iText PDF
 Spring Boot – Enable SSL (HTTPs)
 Spring Boot – Basic Authentication
 Spring Boot – In Memory Basic Authentication
 Spring Boot – Security MySQL Database Integration
 Spring Boot – Redis Cache – Redis Server
 Spring Boot – Hazelcast Cache
 Spring Boot – EhCache
 Spring Boot – Kafka Producer
 Spring Boot – Kafka Consumer
 Spring Boot – Kafka JSON Message to Kafka Topic
 Spring Boot – RabbitMQ Publisher
 Spring Boot – RabbitMQ Consumer
 Spring Boot – SOAP Consumer
 Spring Boot – Soap WebServices
 Spring Boot – Batch Csv to Database
 Spring Boot – Eureka Server
 Spring Boot – MockMvc JUnit
 Spring Boot – Docker Deployment