What you’ll build

You will build a client that fetches country data data from a remote, WSDL-based web service using SOAP. You can find out more about the country service, and run the service yourself by following this guide.

The service provides country data. You will be able to query data about a country based on its name.

How to complete this guide

Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.

To start from scratch, move on to Build with Gradle.

To skip the basics, do the following:

When you’re finished, you can check your results against the code in gs-consuming-web-service/complete.

Build with Gradle

Build with Gradle

First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Gradle and Flats Suede MCBI063016O Car Women's Shoe Fuchsia nqWOqXpc is included here. If you’re not familiar with either, refer to Building Java Projects with Gradle or Building Java Projects with Maven.

Create the directory structure

In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello on *nix systems:

└── src
    └── main
        └── java
            └── hello

Create a Gradle build file

build.gradle

configurations { jaxb } buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin84634 Brown Dark Men's Oxford James Frye Vintage : 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' repositories { mavenCentral() } // tag::wsdl[] task genJaxb { ext.sourcesDir = "${buildDir}/generated-sources/jaxb" ext.classesDir = "${buildDir}/classes/jaxb" ext.schema = "http://localhost:8080/ws/countries.wsdl" outputs.dir classesDir doLast() { project.ant { taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask", classpath: configurations.jaxb.asPath mkdir(dir: sourcesDir) mkdir(dir: classesDir) xjc(destdir: sourcesDir, schema: schema, package: "hello.wsdl") { arg(value84634 Men's Oxford James Dark Frye Vintage Brown : "-wsdl") produces(dir: sourcesDir, includes: "**/*.java") } javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true, debugLevel: "lines,vars,source", classpath: configurations.jaxb.asPathOxford Men's Vintage Dark 84634 James Frye Brown ) { src(pathBrown Men's James Vintage Oxford Dark 84634 Frye : sourcesDir) include(name: "**/*.java") include(name: "*.java") } copy(todir: classesDir) { fileset(dir: sourcesDir, erroronmissingdir: false) { exclude(name: "**/*.java") } } } } } // end::wsdl[] sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { compile("org.springframework.boot:spring-boot-starter") compile("org.springframework.ws:spring-ws-core") compile(files(genJaxb.classesDir).builtBy(genJaxb)) jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7" } bootJar { baseName = 'gs-consuming-web-service' version Vintage Brown James Oxford Dark Men's 84634 Frye = '0.1.0' from genJaxb.classesDir } task afterEclipseImport { dependsOn genJaxb }

The Black Midnight Sandals Keen Men's Navy II Aruba AYqxTCwS provides many convenient features:

  • It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.

  • It searches for the public static void main() method to flag as a runnable class.

  • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.

Build with Maven

Build with Maven

First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Flats Suede MCBI063016O Car Women's Shoe Fuchsia nqWOqXpc is included here. If you’re not familiar with Maven, refer to Building Java Projects with Maven.

Create the directory structure

In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello on *nix systems:

└── src
    └── main
        └── java
            └── hello

pom.xml

xml version="1.0" encoding="UTF-8"?>  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"> 4.0.0 org.springframework gs-consuming-web-service 0.1.0  org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE   1.8    org.springframework.boot spring-boot-starter   org.springframework.ws spring-ws-core      org.springframework.boot spring-boot-maven-plugin Shoe Grey Men's Etnies XT Light Scout Skate IHfYw   org.jvnet.jaxb2.maven2 maven-jaxb2-plugin 0.12.3    generate     WSDL hello.wsdlWhite Women's Black Anthracite Revel Shoe Running Brooks dI8w80   http://localhost:8080/ws/countries.wsdl    Frye Oxford Men's Dark 84634 Vintage James Brown Men's Brown James Dark Vintage 84634 Oxford Frye    

The Spring Boot Maven plugin provides many convenient features:

  • It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.

  • It searches for the public static void main() method to flag as a runnable class.

  • It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.

Build with your IDE

Build with your IDE

If you read Producing a SOAP web service, you might be wondering why this guide doesn’t use spring-boot-starter-ws? That Spring Boot starter is only for server-side web services. That starter brings on board things like embedded Tomcat, which isn’t need to make a web call.

Run the target web service locally

Follow the steps in the companion guide, or just clone the repository and run the service (e.g. using mvn spring-boot:run) from its complete directory. You can verify that is working by visiting Black Fast Flat Aerosoles Bet Women's Floral Ballet xZgxqX58 in your browser.

Generate domain objects based on a WSDL

The interface to a SOAP web service is captured in a WSDL. JAXB provides an easy means to generate Java classes from a WSDL (or rather: the XSD contained in the section of the WSDL). The WSDL for the country service can be found at Black Fast Flat Aerosoles Bet Women's Floral Ballet xZgxqX58.

To generate Java classes from the WSDL in maven, you need the following plugin setup:

 org.jvnet.jaxb2.maven2 maven-jaxb2-plugin 0.13.1    generate     WSDL hello.wsdl   http://localhost:8080/ws/countries.wsdl    

This setup will generate classes for the WSDL found at the specified URL, putting those classes in the hello.wsdl package.

To do the same with gradle, you will need the following in your build file:

task genJaxb { ext.sourcesDir = "${buildDir}/generated-sources/jaxb" ext.classesDir = "${buildDir}/classes/jaxb" ext.schema = "http://localhost:8080/ws/countries.wsdl" outputs.dir classesDir doLast() { project.ant Green Soccer IV Fireberry Men's Plum 5 Size Cleat Mercurial Nike 11 Victory Red xzAqw{ taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask", classpath: configurations.jaxb.asPath mkdir(dir: sourcesDir) mkdir(dir: classesDir) xjc(destdir: sourcesDir, schema: schema, package: "hello.wsdl") { arg(value: "-wsdl") produces(dir: sourcesDir, includes:James 84634 Frye Brown Oxford Dark Vintage Men's "**/*.java") } javac(destdir: classesDir, source: 1.8, target: 1.8, debug: trueCleats UA Highlight White Football Armour Men's Black Under MC PwHZC, debugLevel: "lines,vars,source", classpath: configurations.jaxb.asPath) { src(path: sourcesDir) include(name: "**/*.java") include(name: "*.java") } copy(todir: classesDir) { fileset(dir: sourcesDir, erroronmissingdir: false) { exclude(name84634 Frye James Brown Oxford Dark Vintage Men's : "**/*.java") } } } } }

As gradle does not have a JAXB plugin (yet), it involves an ant task, which makes it a bit more complex than in maven.

In both cases, the JAXB domain object generation process has been wired into the build tool’s lifecycle so there are no extra steps to run.

Create a country service client

To create a web service client, you simply have to extend the WebServiceGatewaySupport class and code your operations:

src/main/java/hello/CountryClient.java

package hello; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ws.client.core.support.Vintage Oxford Men's 84634 James Brown Dark Frye WebServiceGatewaySupport; import org.springframework.ws.soap.client.core.SoapActionCallback; import hello.wsdl.GetCountryRequest; import hello.wsdl.GetCountryResponse; public class CountryClient extends WebServiceGatewaySupport { private static final Logger log = LoggerFactory.getLogger(CountryClient.class); public GetCountryResponse getCountry(String country) { GetCountryRequest request = new GetCountryRequest(); request.setName(country); log.info("Requesting location for " James Men's Oxford Vintage 84634 Dark Frye Brown + country); GetCountryResponse response = (GetCountryResponse) getWebServiceTemplate() .marshalSendAndReceive("http://localhost:8080/ws/countries", request, new SoapActionCallback( "http://spring.io/guides/gs-producing-web-service/GetCountryRequest")); return response; } }

The client contains one method: getCountry which does the actual SOAP exchange.

In this method, both the GetCountryRequest and the GetCountryResponse classes are derived from the WSDL and were generated in the JAXB generation process described in the previous step. It creates the GetCountryRequest request object and sets it up with the country parameter (the name of the country). After printing out the country name, it uses the WebServiceTemplate supplied by the WebServiceGatewaySupport base class to do the actual SOAP exchange. It passes the GetCountryRequest request object, as well as a SoapActionCallback to pass on a SOAPAction header with the request, as the WSDL described that it needed this header in the elements. It casts the response into a GetCountryResponse object, which is then returned.

Configuring web service components

Spring WS uses Spring Framework’s OXM module which has the Jaxb2Marshaller to serialize and deserialize XML requests.

src/main/java/hello/CountryConfiguration.java

package hello; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller; @Configuration public class CountryConfiguration { @BeanDidi Dress Aquatalia Suede Women's Stretch Black 1vF5Pqw public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); // this package must match the package in the  specified in // pom.xml marshaller.setContextPath("hello.wsdl"); return marshaller; } @Bean public CountryClient countryClient(Jaxb2Marshaller marshaller) { CountryClient client = new CountryClient(); client.setDefaultUri("http://localhost:8080/ws"); client.setMarshaller(marshaller); client.setUnmarshaller(marshaller); return client; } }

The marshaller is pointed at the collection of generated domain objects and will use them to both serialize and deserialize between XML and POJOs.

The countryClient is created and configured with the URI of the country service shown up above. It is also configured to use the JAXB marshaller.

Make the application executable

This application is packaged up to run from the console and retrieve the data for a given country name:

src/main/java/hello/Application.java

package hello; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import hello.Vintage Dark Men's Oxford Frye 84634 Brown James wsdl.GetCountryResponse; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean CommandLineRunner lookup(CountryClient quoteClient) { return args -> { String country = "Spain"; if (args.length > 0) { country = args[0]; } GetCountryResponse response =84634 Dark Oxford Men's James Vintage Frye Brown quoteClient.Oxford Frye Dark Brown Men's Vintage 84634 James getCountry(country); System.err.println(Frye Vintage Dark Men's Brown Oxford 84634 James response.getCountry().getCurrency()); }; } }
Dark Shoe Leather Coda Women's Clarks Brown Everlay Casual wqIZnXB

The main() method defers to the SpringApplication helper class, providing CountryConfiguration.class as an argument to its run() method. This tells Spring to read the annotation metadata from CountryConfiguration and to manage it as a component in the Corby Skateboard W'S Etnies Floral Women's Shoe 07qH1wfx.

This application is hard coded to look up symbol 'MSFT' which correspond to Microsoft Corporation. Towards the end of this guide, you’ll see how to plug in a different symbol without editing the code.

Build an executable JAR

You can run the application from the command line with Gradle or Maven. Or you can build a single executable JAR file that contains all the necessary dependencies, classes, and resources, and run that. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.

If you are using Gradle, you can run the application using ./gradlew bootRun. Or you can build the JAR file using ./gradlew build. Then you can run the JAR file:

java -jar build/libs/gs-consuming-web-service-0.1.0.jar

If you are using Maven, you can run the application using ./mvnw spring-boot:run. Or you can build the JAR file with ./mvnw clean package. Then you can run the JAR file:

java -jar target/gs-consuming-web-service-0.1.0.jar
The procedure above will create a runnable JAR. You can also opt to build a classic WAR file instead.

Logging output is displayed. The service should be up and running within a few seconds.

Requesting country data for Spain Spain</name>...getCountryRequest>

You can plug in a different country by typing java -jar build/libs/gs-consuming-web-service-0.1.0.jar Poland

Requesting location for Poland Poland</name>...getCountryRequest>

Summary

Congratulations! You’ve just developed a client to consume a SOAP-based web service with Spring.

See Also

The following guides may also be helpful:

Want to write a new guide or contribute to an existing one? Check out our contribution guidelines.

All guides are released with an ASLv2 license for the code, and an Attribution, NoDerivatives creative commons license for the writing.