Consume Soap services using spring boot

Sat, May 30, 2020

Read in 5 minutes

In this article ,we will explore how to consume SOAP service.

As REST webservice is dominating the market for the past few years and I couldn’t get opportunity to consume the SOAP service .Currently I am involving in a project where i have to consume SOAP service from the vendor product .So I brush up about the SOAP basics and I would like to share that knowledge to a fellow engineer through this blog.

SOAP1

Webservices :

The webservices are created to exchange the data between the two different system . Two types of webservices created are ,

SOAP Basics :

WSDL :

SOAP2

WSDL Tags :

SOAP3

Steps to Consume a SOAP service :

Step :1 - Create Spring boot Project and Get the WSDL

Got the WSDL and create spring boot project ,and select the dependencies - Spring web and Spring Web Services If want to know about creating boot project in detail you can follow the below URL https://www.theprogrammerguide.com/spring/springboot_starter/

SOAP4

Step :2 Convert the WSDL to Stub

<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>com.theprogrammerguide.consumesoap.stub</generatePackage>
<generateDirectory>${project.basedir}/src/main/java</generateDirectory>
<schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
<schemaIncludes>
<include>*.wsdl</include>
</schemaIncludes>
</configuration>
</plugin>

Step 3 : Understand the request ,response and their types

SOAP5 Update the SOAP URL and click ok for the next dialogue boxes .

SOAP6 Now you can see all the operations list on the screen and click any one of the request . And update <blz:blz>46062817</blz:blz>.Then click the green triangle to run .

SOAP7

And the response is ,

SOAP8

Step:4 Form the request object by mapping data and call the soap uri

SoapClient.java

package com.theprogrammerguide.consumesoap;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import com.theprogrammerguide.consumesoap.stub.GetBankResponseType;
import javax.xml.bind.JAXBElement;
public class SoapClient extends WebServiceGatewaySupport {
public GetBankResponseType getBank(String url, Object request){
JAXBElement res = (JAXBElement) getWebServiceTemplate().marshalSendAndReceive(url, request);
return (GetBankResponseType) res.getValue();
}
}

BeanConfig.java

@Configuration
public class BeanConfig {
@Bean
public Jaxb2Marshaller marshaller()  {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.theprogrammerguide.consumesoap.stub");
return marshaller;
}
@Bean
public SoapClient soapConnector(Jaxb2Marshaller marshaller) {
SoapClient client = new SoapClient();
client.setDefaultUri("http://www.thomas-bayer.com/axis2/services/BLZService");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}

Here in the BeanConfig , we create Jaxb2Marshaller object and set the context path .This Jaxb2Marshaller object is used to create java object from the response XML.

Test the Spring Boot code and get the Response :

To Test the spring boot create a controller and call the SOAP client and pass the request data. Here you have to get the code as a query param .So ,using this controller you can map a endpoint and test the API.

package com.theprogrammerguide.consumesoap.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.theprogrammerguide.consumesoap.SoapClient;
import com.theprogrammerguide.consumesoap.stub.DetailsType;
import com.theprogrammerguide.consumesoap.stub.GetBankResponseType;
import com.theprogrammerguide.consumesoap.stub.GetBankType;
import com.theprogrammerguide.consumesoap.stub.ObjectFactory;

@RestController
@RequestMapping("/")
public class BlzController {

@Autowired
private SoapClient soapClient;

@GetMapping
public DetailsType sum(@RequestParam String code) {
ObjectFactory objectFactory = new ObjectFactory();
GetBankType type = new GetBankType();
type.setBlz(code);
// BlzServiceAdapter blzServiceAdapter=new BlzServiceAdapter();

GetBankResponseType response = soapClient.getBank("http://www.thomas-bayer.com/axis2/services/BLZService",
objectFactory.createGetBank(type));
return response.getDetails();
}
}

Test with the URL :http://localhost:8080/?code=46062817