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"

    <name>SPRING-WS - ${project.artifactId}</name>




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

public class SoapClientConfig {

    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        return marshaller;

    public BeerClient weatherClient(Jaxb2Marshaller marshaller) {
        BeerClient client = new BeerClient();
        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());




You may also like...

  • Robert Cuong

    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?

  • Subhobroto Roy

    Thank you very much for this awesome tutorial.