Introduction
SOAP (Simple Object Access Protocol) is a protocol for exchanging structured information in web services. While RESTful services dominate modern APIs, SOAP remains essential in enterprise applications due to its robustness, security, and built-in support for transactions.
In this guide, we’ll explore how to consume a SOAP web service in Spring Boot using Spring Web Services (Spring-WS) and JAX-WS.
Prerequisites
Before starting, ensure you have the following:
- Java 8 or later
- Spring Boot 2.x or 3.x
- Maven or Gradle
- A WSDL file or an available SOAP web service
Setting Up the Spring Boot Project
We will create a Spring Boot project to consume a SOAP service using Spring Web Services.
Step 1: Add Dependencies
Include the necessary dependencies in pom.xml
:
<dependencies>
<!-- Spring Web Services -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- JAXB for marshalling and unmarshalling -->
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
<!-- JAX-WS (optional, for Java-generated classes from WSDL) -->
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
</dependency>
</dependencies>
Step 2: Generate Java Classes from WSDL
To interact with the SOAP service, we need Java classes that map to the request and response structure. Use the wsimport
tool:
wsimport -keep -p com.example.client -d ./src/main/java http://example.com/service?wsdl
This generates the required classes in the com.example.client
package.
Step 3: Configure the SOAP Client
Create a SOAPConnector
class to handle communication:
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import org.springframework.stereotype.Component;
@Component
public class SOAPConnector extends WebServiceGatewaySupport {
public Object callWebService(String url, Object request){
return getWebServiceTemplate().marshalSendAndReceive(url, request, new SoapActionCallback("http://example.com/action"));
}
}
Step 4: Define a Service Layer
Create a service class to consume the SOAP service:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SoapClientService {
@Autowired
private SOAPConnector soapConnector;
public ResponseType callSoapService(RequestType request) {
return (ResponseType) soapConnector.callWebService("http://example.com/service", request);
}
}
Step 5: Create a REST Controller for Testing
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SoapController {
@Autowired
private SoapClientService soapClientService;
@GetMapping("/invokeSoap")
public ResponseType invokeSoap(@RequestParam String param) {
RequestType request = new RequestType();
request.setParam(param);
return soapClientService.callSoapService(request);
}
}
Testing the SOAP Client
Start the Spring Boot application and call the SOAP service using a REST client like Postman:
GET http://localhost:8080/invokeSoap?param=sampleValue
If configured correctly, the response should return the SOAP service result.
External Resources
FAQs
- What is SOAP in Java?
SOAP is a protocol for exchanging structured XML-based messages over a network, commonly used in web services. - Why use SOAP instead of REST?
SOAP offers enhanced security, transaction support, and is ideal for enterprise-level applications requiring strict contracts. - Can I consume a SOAP service in Spring Boot without WSDL?
Yes, but using a WSDL allows automatic class generation, making integration easier. - What is JAXB, and why is it used?
JAXB (Java Architecture for XML Binding) is used for converting Java objects to XML and vice versa. - How does Spring Boot handle SOAP requests?
Spring Boot uses Spring Web Services (spring-ws
) for sending and receiving SOAP messages. - What is the difference between JAX-WS and JAX-RS?
JAX-WS is for SOAP-based web services, whereas JAX-RS is for RESTful web services. - How do I debug SOAP responses in Spring Boot?
Enable logging withlogging.level.org.springframework.ws=DEBUG
inapplication.properties
. - Can I use Spring Boot with Apache CXF for SOAP services?
Yes, Apache CXF provides additional features for working with SOAP web services. - Is SOAP still relevant today?
Yes, many enterprises still use SOAP for secure and transactional web services. - How do I handle authentication in SOAP services?
Use WS-Security with security headers or integrate with OAuth for authentication.
Conclusion
Consuming SOAP services in Spring Boot is straightforward with Spring Web Services and JAX-WS. By following this guide, you can integrate SOAP-based APIs seamlessly into your Java applications. With the right setup and best practices, you can ensure robust, secure, and maintainable web service consumption.