Spring WS Consume Soap Service from WSDL
This tutorial Spring WS Consume Soap Service from WSDL shows you how to Consume a Soap Web Service from a WSDL (Web Service Description Language) file. Spring WS provides a simple client-side Web Service API. It uses the WebServiceTemplate
obtained from the WebServiceGatewaySupport
for sending and receiving Soap Messages. In our previous example we saw how to produce soap web service using spring ws, in this example we will implement a client for this service.
Maven Configuration
For generating the Java Classes from WSDL we use the maven-jaxb2-plugin. We configure the WSDL schema location of the service which we want to implement and the generatedPackage specifying in which package structure the Java Classes will be created.
<?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.memorynotfound.spring.ws</groupId>
<artifactId>consume-soap-web-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>SPRING-WS - ${project.artifactId}</name>
<url>https://memorynotfound.com</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>com.memorynotfound.beer</generatePackage>
<schemas>
<schema>
<url>http://localhost:8080/ws/beers.wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
</plugins>
</build>
</project>
Generating Java Classes From WSDL
After running the following command, the Java Classes will be generated in the target/generated-sources/xjc/<package-name> folder.
mvn package
Spring WS Consume Soap Service Client
We can extend from the WebServiceGatewaySupport
from which we obtain the WebServiceTemplate
which we can use for marshalling, sending and receiving the requests and responses.
package com.memorynotfound.client;
import com.memorynotfound.beer.GetBeerRequest;
import com.memorynotfound.beer.GetBeerResponse;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
public class BeerClient extends WebServiceGatewaySupport {
public GetBeerResponse getBeer(int id) {
GetBeerRequest request = new GetBeerRequest();
request.setId(id);
return (GetBeerResponse) getWebServiceTemplate().marshalSendAndReceive(request);
}
}
Spring Java Configuration
The @Configuration
annotations tells spring that this Java Class is used as configuration file.
We create a Jaxb2Marshaller
to automatically create Java Objects to and from xml request/response. We need to register a contextPath, this is the location of our JAX-B annotated resources. In this example they are created in the com.memorynotfound.beer
package specified in the maven configuration.
Finally we configure the BeerClient
which extends the WebServiceGatewaySupport
from where we must set the defaultUri, marshaller and unmarshaller.
package com.memorynotfound.client;
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();
marshaller.setContextPath("com.memorynotfound.beer");
return marshaller;
}
@Bean
public BeerClient weatherClient(Jaxb2Marshaller marshaller) {
BeerClient client = new BeerClient();
client.setDefaultUri("http://localhost:8080/ws/beer");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
Testing Soap Client
We bootstrap the application using the AnnotationConfigApplicationContext
which we pass in our SoapClientConfig
Spring Java Configuration file. Then we obtain our BeerClient
which we can use to perform our Soap Service.
package com.memorynotfound.client;
import com.memorynotfound.beer.GetBeerResponse;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class RunClient {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SoapClientConfig.class);
BeerClient client = context.getBean(BeerClient.class);
GetBeerResponse response = client.getBeer(1);
System.out.println("Beer response: " + response.getBeer().getName());
}
}
References
- maven-jaxb2-plugin Doc
- WebServiceGatewaySupport JavaDoc
- WebServiceTemplate JavaDoc
- Spring WS API
- Spring WS Reference Documentation
Thanks for tutorial.
I following this example code to connect my SOAP API, Endpoint is: http://localhost/nl/jsp/soaprouter.jsp
I changed client.setDefaultUri(“http://localhost/nl/jsp/soaprouter.jsp”);
mvn package for generate JavaClass
and try to call to endpoint. I use SOAP UI, it works… but when i try call via this example have errors messages:
org.springframework.ws.soap.client.SoapFaultClientException: The SOAP service ” is not implemented by the server.
What am i missing?
Thank you very much for this awesome tutorial.