0% found this document useful (0 votes)
717 views11 pages

Spring Boot SOAP Client Tutorial

This document provides instructions for consuming SOAP web services using a Spring Boot client application. It begins by listing several tutorials on publishing and consuming SOAP web services using Spring frameworks. It then outlines the steps to create a SOAP client, which includes generating domain objects from a WSDL, creating a client class, configuring marshalling/unmarshalling, and running the client to retrieve movie data. Code examples are provided for common classes and configurations needed to setup the SOAP client.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
717 views11 pages

Spring Boot SOAP Client Tutorial

This document provides instructions for consuming SOAP web services using a Spring Boot client application. It begins by listing several tutorials on publishing and consuming SOAP web services using Spring frameworks. It then outlines the steps to create a SOAP client, which includes generating domain objects from a WSDL, creating a client class, configuring marshalling/unmarshalling, and running the client to retrieve movie data. Code examples are provided for common classes and configurations needed to setup the SOAP client.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Project Structure
  • Tools and Environment
  • Steps to create SOAP Client

Consume Spring SOAP web services

using client application – Part II


 latha  Spring Boot, Web Services  August 3, 2018

In this post, we will learn how to consume SOAP web services by creating a simple client
application. We will use this tutorial : Publish SOAP Web services using Spring Boot – Part 1  to get
WSDL, which is used in our following client application.
Here are list of post on SOAP web services using spring framework

1. Publish SOAP web services – perform CRUD operation and consume SOAP web services
using SOAP UI : We will explore these topic in this post – Publish SOAP Web services using
Spring Boot – Part 1
2. Consume SOAP web services using client application – We learn about this topic in here
3. Exception handling in SOAP web services-Spring SOAP Web services – Add SOAP Fault
Exception handling – Part III
4. Exception handling in CRUD SOAP web services – Spring SOAP Web services – Add SOAP
Fault Exception handling for CRUD operations – Part IV
5. Securing SOAP web services – In upcoming tutorial
6. Testing SOAP web services – In upcoming tutorial

List of Contents
1. Tools and Environment
2. Project Structure
3. Steps to create SOAP Client
4. Download
5. References

1. Tools and Environment


Following tools and environments are used to consume SOAP web services in Spring Boot

 Spring Tool Suite (version: [Link])


 Spring Boot (version: [Link])
 Spring (version: [Link])
 Spring data jpa (version: [Link])
 Java (version: 8)
 MySQL (version: 5.1.46)
 Hibernate (version: [Link])
 Maven (version: 3.5.2)

2. Project Structure
3. Steps to create SOAP Client
1. Create a server application to publish/produce soap web services
1. Run the server application
2. Test WSDL in the web browser ( We need this WSDL location to create client
application)
2. Create a client application to consume SOAP web services
3. Run and test the client application

Step 3.1: Create a server application to publish/produce


soap web services
Follow this tutorial to create an application to produce SOAP web services or download project in
download section in Publish and Consume SOAP Web services using Spring Boot – Part 1

Step 3.1.1: Run the server application


Right click on the project and do Run As -> Spring Boot App. The application will start running at
default port 8080 or run the service (e.g. using mvn spring-boot:run) from its complete directory
Step 3.1.2: Test WSDL in the web browser
Verify that the application is working by visiting [Link] in your browser
 
 

Step 3.2: Create a client application to consume SOAP


web services
Steps to create client application

1. Create a new Spring Starter Project


2. Edit [Link] to generate domain objects based on a WSDL. Add following dependency and
plugin
1. spring-ws-core
2. maven-jaxb2-plugin
3. Do Maven -> Update Project to generate Java sources classes from WSDL
4. Create a web service client that extends WebServiceGatewaySupport
5. Configuring web service components
1. Spring WS uses Spring Framework’s OXM module which has
the Jaxb2Marshaller to serialize and deserialize XML requests.
6. Make the application executable
Step 3.2.1 – Create a new Spring Starter Project

 
Step 3.2.2: Edit [Link] to generate domain objects
based on a WSDL
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>

<groupId>[Link]</groupId>
<artifactId>SpringBoot_SOAP_Client_Movies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>SpringBoot_SOAP_Client_Movies</name>
<description>WebService Example</description>

<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>[Link]</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<[Link]>UTF-8</[Link]>
<[Link]>UTF-
8</[Link]>
<[Link]>1.8</[Link]>
</properties>

<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>[Link].jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>

<generatePackage>[Link]</generatePackage>
<schemas>
<schema>
<url>[Link]
</schema>
</schemas>
</configuration>
</plugin>

</plugins>
</build>

</project>

Step 3.2.3: Do Maven -> Update Project to generate Java


sources classes from WSDL
Do maven update after updating [Link]

Maven plugin will generate java source classes under “[Link].gs_ws”. This package
name is specified in [Link] file
 
Alternatively, running the following command in parent directory, the Java Classes will be generated
in the target/generated-sources/jaxb/<package-name> folder. <package-name> is specified in
the [Link] file
mvn package

Step 3.2.4: Create a web service client that extends


WebServiceGatewaySupport
[Link]
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

public class MovieClient extends WebServiceGatewaySupport {

private static final Logger log =


[Link]([Link]);

public GetMovieByIdResponse getMovieById(Long id) {


GetMovieByIdRequest request = new GetMovieByIdRequest();
[Link](id);

[Link]("Requesting Movie By id= " + id);


return (GetMovieByIdResponse)
getWebServiceTemplate().marshalSendAndReceive(request);

Step 3.2.5: Configuring web service components


Spring WS uses Spring Framework’s OXM module which has the Jaxb2Marshaller to serialize and
deserialize XML requests.
[Link]
import [Link];
import [Link];
import [Link].Jaxb2Marshaller;

@Configuration
public class SoapClientConfig {

@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this package must match the package in the specified in
// [Link]
[Link]("[Link]");
return marshaller;
}

@Bean
public MovieClient movieClient(Jaxb2Marshaller marshaller) {
MovieClient client = new MovieClient();
[Link]("[Link]
[Link](marshaller);
[Link](marshaller);
return client;
}

Step 3.2.6: Make the application executable


This application is packaged up to run from the console and retrieve the data for a given movie id
[Link]
import [Link];
import [Link];

public class RunClient {

public static void main(String[] args) {


AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext([Link]);
MovieClient client = [Link]([Link]);
GetMovieByIdResponse response = [Link](new Long(103));
[Link]("response: Movie id="+
[Link]().getMovieId()+", title=" + [Link]().getTitle()
+ ", category="+ [Link]().getCategory());
}

Note: This application only retrieves information of a movie by Id for simplicity. You can implement
rest of CRUD operations by following this example

Step 3.3: Run and test the client application


Run the “RunClient” as Java application and see the results

Common questions

Powered by AI

The key steps involve creating a new Spring Starter Project, editing the pom.xml file to add dependencies like spring-ws-core and plugins such as maven-jaxb2-plugin, and performing Maven Update Project to generate Java source classes from the WSDL. You then create a web service client that extends WebServiceGatewaySupport and configure the web service components using the OXM module for XML serialization/deserialization. Finally, make the application executable .

In pom.xml, integrate SOAP web service capabilities by including dependencies such as spring-ws-core and the maven-jaxb2-plugin for processing WSDL files. Define the plugin configuration to specify the WSDL schema location and the target package for generating Java classes, ensuring proper integration and code generation from SOAP service descriptions .

Spring Boot facilitates rapid development of SOAP web service clients by offering embedded servers, auto-configuration, and simplifying dependency management through tools like Maven. These features enhance productivity and ease the development process. However, potential limitations include a steep learning curve for developers unfamiliar with Spring's extensive ecosystem, and increased memory usage from layered abstractions if not managed correctly, which could affect performance in resource-constrained environments .

WebServiceGatewaySupport acts as a support base class for executing web service operations using WebServiceTemplate. It abstracts the underlying complexity of sending and receiving SOAP messages by providing convenient sendAndReceive methods. These methods marshal the request and unmarshal the response, simplifying the invocation of web service operations for the developer, ensuring reliable communication with web service endpoints .

Maven offers significant benefits like automated dependency management, project consistency through standardized builds, and ease of integration with continuous integration tools, which are advantageous for complex SOAP client projects. However, challenges include potential version conflicts, the complexity of plugin configuration, and possible slow build times if not optimized, which can impede development efficiency if mismanaged .

Jaxb2Marshaller in Spring WS facilitates XML request handling by providing serialization and deserialization capabilities. It allows the conversion of Java objects to XML and vice versa, leveraging JAXB (Java Architecture for XML Binding). This conversion is essential for the SOAP client to communicate effectively with web services by correctly interpreting XML requests and responses .

A developer might extend WebServiceGatewaySupport because it simplifies the integration with SOAP web services by providing streamlined methods for sending and receiving messages. It encapsulates the WebServiceTemplate, facilitating operations through concise method calls and error handling, thus reducing boilerplate code and enhancing maintainability, which is particularly beneficial in complex enterprise applications .

Spring Tool Suite (STS) streamlines the development of SOAP web services by providing an integrated development environment that supports Spring Boot's features. It includes specialized tools and extensions for managing Spring projects, facilitating code navigation, refactoring, and debugging, thereby improving developer productivity and reducing errors .

The project structure supports the development lifecycle by organizing source files, configuration, and generated code effectively, ensuring maintainability and scalability. It typically includes distinct folders for generated Java classes, source code, and configuration (including pom.xml) allowing for clear separation of concerns, which facilitates easier code navigation and modifications, aids in continuous integration environments, and streamlines the build and deployment process .

Testing WSDL in the web browser verifies that the server-side application correctly publishes the SOAP services. It ensures that the service description is accessible and accurately represents available operations and messages, which are vital for generating client-side classes and establishing reliable interactions with services. This step is crucial for debugging configuration issues before client implementation begins .

You might also like