📌 What is SOAP Web Service?
SOAP (Simple Object Access Protocol) is a protocol for exchanging structured information in web services. In Spring Boot, you can create SOAP web services using Spring Web Services (Spring-WS).
🚀 Example: Spring Boot SOAP Web Service
1. Add Dependencies (pom.xml
)
For a Maven project, add:
<dependencies>
<!-- Spring Web Services -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- JAXB for XML binding -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
</dependencies>
2. Create an XML Schema (src/main/resources/courses.xsd
)
This defines the request and response structure.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://in28minutes.com/courses"
xmlns:tns="http://in28minutes.com/courses"
elementFormDefault="qualified">
<xs:element name="getCourseDetailsRequest" type="tns:getCourseDetailsRequest"/>
<xs:element name="getCourseDetailsResponse" type="tns:getCourseDetailsResponse"/>
<xs:complexType name="getCourseDetailsRequest">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getCourseDetailsResponse">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
3. Generate Java Classes from XSD
Run:
mvn jaxb2:xjc
This generates request/response classes (like GetCourseDetailsRequest
, GetCourseDetailsResponse
).
4. Create Endpoint
package com.example.demo.soap;
import com.in28minutes.courses.GetCourseDetailsRequest;
import com.in28minutes.courses.GetCourseDetailsResponse;
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 CourseEndpoint {
private static final String NAMESPACE_URI = "http://in28minutes.com/courses";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCourseDetailsRequest")
@ResponsePayload
public GetCourseDetailsResponse processCourseDetailsRequest(
@RequestPayload GetCourseDetailsRequest request) {
GetCourseDetailsResponse response = new GetCourseDetailsResponse();
response.setId(request.getId());
response.setName("Spring Boot");
response.setDescription("Learn Spring Boot SOAP Web Service");
return response;
}
}
5. Configure Web Service (WebServiceConfig.java
)
package com.example.demo.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;
@EnableWs
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext context) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(context);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*");
}
@Bean(name = "courses")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema coursesSchema) {
DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
definition.setPortTypeName("CoursePort");
definition.setLocationUri("/ws");
definition.setTargetNamespace("http://in28minutes.com/courses");
definition.setSchema(coursesSchema);
return definition;
}
@Bean
public XsdSchema coursesSchema() {
return new SimpleXsdSchema(new ClassPathResource("courses.xsd"));
}
}
6. Run Application
Start the Spring Boot app, then open:
📌 WSDL available at:
http://localhost:8080/ws/courses.wsdl
7. Example SOAP Request
Send via Postman or SOAP UI:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://in28minutes.com/courses">
<soapenv:Header/>
<soapenv:Body>
<tns:getCourseDetailsRequest>
<tns:id>1</tns:id>
</tns:getCourseDetailsRequest>
</soapenv:Body>
</soapenv:Envelope>
Response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getCourseDetailsResponse xmlns:ns2="http://in28minutes.com/courses">
<ns2:id>1</ns2:id>
<ns2:name>Spring Boot</ns2:name>
<ns2:description>Learn Spring Boot SOAP Web Service</ns2:description>
</ns2:getCourseDetailsResponse>
</soap:Body>
</soap:Envelope>