Consume Spring SOAP Web Services Using Client Application - Part II
Consume Spring SOAP Web Services Using Client Application - Part II
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
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.2.2: Edit pom.xml to generate domain objects
based on a WSDL
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javaspringclub</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jvnet.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>movies.wsdl</generatePackage>
<schemas>
<schema>
<url>http://localhost:8080/ws/movies.wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
</plugins>
</build>
</project>
Maven plugin will generate java source classes under “com.javaspringclub.gs_ws”. This package
name is specified in pom.xml 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 pom.xml file
mvn package
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import movies.wsdl.GetMovieByIdRequest;
import movies.wsdl.GetMovieByIdResponse;
@Configuration
public class SoapClientConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this package must match the package in the specified in
// pom.xml
marshaller.setContextPath("movies.wsdl");
return marshaller;
}
@Bean
public MovieClient movieClient(Jaxb2Marshaller marshaller) {
MovieClient client = new MovieClient();
client.setDefaultUri("http://localhost:8080/ws/movies");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
Note: This application only retrieves information of a movie by Id for simplicity. You can implement
rest of CRUD operations by following this example