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

1. Tools and Environment

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

  • Spring Tool Suite (version: 3.9.4.RELEASE)
  • Spring Boot (version: 2.0.4.RELEASE)
  • Spring (version: 5.0.3.RELEASE)
  • Spring data jpa (version: 2.0.9.RELEASE)
  • Java (version: 8)
  • MySQL (version: 5.1.46)
  • Hibernate (version: 5.2.17.Final)
  • 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 http://localhost:8080/ws/movies.wsdl 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 pom.xml 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 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>

Step 3.2.3: Do Maven -> Update Project to generate Java sources classes from WSDL

Do maven update after updating pom.xml

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

Step 3.2.4: Create a web service client that extends WebServiceGatewaySupport

MovieClient.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import movies.wsdl.GetMovieByIdRequest;
import movies.wsdl.GetMovieByIdResponse;

public class MovieClient extends WebServiceGatewaySupport {

	private static final Logger log = LoggerFactory.getLogger(MovieClient.class);

	public GetMovieByIdResponse getMovieById(Long id) {
		GetMovieByIdRequest request = new GetMovieByIdRequest();
		request.setMovieId(id);

		log.info("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.

SoapClientConfig.java

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();
		// 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;
	}

}

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

RunClient.java

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import movies.wsdl.GetMovieByIdResponse;

public class RunClient {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SoapClientConfig.class);
        MovieClient client = context.getBean(MovieClient.class);
        GetMovieByIdResponse response = client.getMovieById(new Long(103));
        System.out.println("response: Movie id="+ response.getMovieType().getMovieId()+", title=" + response.getMovieType().getTitle() + ", category="+ response.getMovieType().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

4. Download

SpringBoot_SOAP_Client_Movies

5. Reference

https://spring.io/guides/gs/consuming-web-service/

About: latha

Hi all, This is Latha. Im fond of learning and teaching java and spring framework in easy steps.


%d bloggers like this: