Here's a sample Java code that calls a WSDL service in a Spring Boot application using Spring Web Services (SWS):
- Add the necessary dependencies to your
pom.xml file:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
</dependencies>
- Create a configuration class to define the Spring bean for the web service template:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
@Configuration
public class WebServiceConfig {
private static final String SERVICE_URL = "http://www.example.com/your_service.wsdl";
@Bean
public YourWebServiceTemplate yourWebServiceTemplate() {
YourWebServiceTemplate webServiceTemplate = new YourWebServiceTemplate();
webServiceTemplate.setDefaultUri(SERVICE_URL);
return webServiceTemplate;
}
public static class YourWebServiceTemplate extends WebServiceGatewaySupport {
public YourWebServiceResponse callWebService(YourWebServiceRequest request) {
// Marshal the request object to XML
// Example using JAXB:
JAXBElement<YourWebServiceRequest> requestElement = new ObjectFactory()
.createYourWebServiceRequest(request);
// Make the web service call
JAXBElement<YourWebServiceResponse> responseElement = (JAXBElement<YourWebServiceResponse>) getWebServiceTemplate()
.marshalSendAndReceive(SERVICE_URL, requestElement,
new SoapActionCallback("http://www.example.com/your_service/your_method"));
// Unmarshal the response XML to an object
YourWebServiceResponse response = responseElement.getValue();
return response;
}
}
}
- Create the Web Service request and response classes using JAXB annotations:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "YourWebServiceRequest")
public class YourWebServiceRequest {
// Request fields and getters/setters
}
@XmlRootElement(name = "YourWebServiceResponse")
public class YourWebServiceResponse {
// Response fields and getters/setters
}
- Create a controller to handle the web service request:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class YourController {
@Autowired
private WebServiceConfig.YourWebServiceTemplate webServiceTemplate;
@PostMapping("/your-service")
public YourWebServiceResponse callWebService(@RequestBody YourWebServiceRequest request) {
return webServiceTemplate.callWebService(request);
}
}
In this example, a YourWebServiceTemplate class is created by extending WebServiceGatewaySupport from Spring Web Services. This class provides convenient methods for making web service calls and handling marshalling/unmarshalling of XML objects.
The callWebService method demonstrates how to send a request to the WSDL service and receive the response. The DefaultUri property is set to the WSDL location, and the marshalSendAndReceive method is used to send the request and receive the response. The SoapActionCallback provides the SOAP action required by the service method.
The controller class uses the YourWebServiceTemplate bean to handle incoming requests and make the web service call. The @PostMapping annotation is used to specify the endpoint and request method. The @RequestBody annotation is used to bind the request body to the YourWebServiceRequest object.
Note: Replace the placeholders like YourWebServiceTemplate, YourWebServiceRequest, YourWebServiceResponse, http://www.example.com/your_service.wsdl, etc., in the code with your own values according to your WSDL service.