0% found this document useful (0 votes)
9 views

soap spring boot

Uploaded by

radu ion
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

soap spring boot

Uploaded by

radu ion
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Here’s an example of a Spring Boot SOAP API implementation.

This example demonstrates


creating a SOAP web service client that consumes an external SOAP service.

Steps:

1. Add Dependencies: Add the necessary dependencies to your pom.xml.

xml
Copy code
<dependencies>
<!-- Spring Web for REST API -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Web Services for SOAP -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<!-- JAXB for XML binding -->


<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>

<!-- wsdl2java plugin for generating classes -->


<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>

2. Generate Java Classes from WSDL: Use a tool like wsdl2java to generate classes
from the WSDL file. Alternatively, include a WSDL file in your resources folder and
add the following plugin in your pom.xml:

xml
Copy code
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>

<wsdl>src/main/resources/service.wsdl</wsdl>
<outputDir>${project.build.directory}/generated-sources/cxf</outputDir>
</wsdlOption>
</wsdlOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

3. Create Configuration for SOAP Client: Create a configuration class for setting up
the SOAP client.

java
Copy code
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Configuration
public class SoapClientConfig {

@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// Replace with the package containing the generated classes from
WSDL
marshaller.setContextPath("com.example.generated");
return marshaller;
}

@Bean
public SoapClient soapClient(Jaxb2Marshaller marshaller) {
SoapClient client = new SoapClient();
client.setDefaultUri("http://example.com/soap-service");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}

4. Create the SOAP Client: Use the generated classes to build a client for making
requests.

java
Copy code
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;

public class SoapClient extends WebServiceGatewaySupport {

public GetResponse getDetails(GetRequest request) {


return (GetResponse) getWebServiceTemplate()
.marshalSendAndReceive(request);
}
}

5. Controller to Call SOAP Service: Create a REST controller to trigger the SOAP
client.
java
Copy code
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 {

private final SoapClient soapClient;

public SoapController(SoapClient soapClient) {


this.soapClient = soapClient;
}

@GetMapping("/getDetails")
public GetResponse getDetails(@RequestParam String param) {
GetRequest request = new GetRequest();
request.setParam(param);

return soapClient.getDetails(request);
}
}

6. Run the Application: Start the Spring Boot application and access the endpoint via
http://localhost:8080/getDetails?param=value.

This example assumes you have a WSDL file and have generated the required classes for
SOAP requests and responses using a tool like wsdl2java. Replace GetRequest,
GetResponse, and URIs with actual values from your SOAP service.

You might also like