Tutorial: Creating a Standalone Web Service Client

« Back to Java Stack Training



Creating a SOAP web service client, particularly against an externally defined web service, has often been fraught with confusion and frustration. But with Java Stack WS support, creating a SOAP web service client couldn't be easier.

In this tutorial, I illustrate some core design patterns for re-usable object oriented software. I also demonstrate several features of the Java Stack that can be used to quickly and easily tackle tasks that have traditionally been viewed as complicated and time consuming.

In particular, this tutorial attempts to answer the following questions:

  1. How do I use the Java Stack from a standalone (non-web) application?
  2. How do I configure Spring for a standalone application?
  3. How do I generate web service client code from a WSDL?
  4. How do I consume a SOAP web service with CXF and Spring?

1. Create a Standalone Java Stack Project

1.1. Download and run the Java Stack Starter.
1.2. Select project type "JAR Application" and enter the following:
Project Name: example-soap-client
Package Name:
Supply a project location
Click Next
1.3. Configure project properties
Choose development infrastructure or keep the default.
Choose Java Platform or keep the default
Choose a project version or keep the default
Choose "Package as executable bundle" if you wish to have your project and all of its dependencies packaged into a single executable jar, otherwise uncheck this option if you plan to use this project as a dependency to other projects.
Click Next
1.4. Review your project settings and click Finish to generate the project.
1.5. Exit Stack Starter and import the new project into the IDE of your choice as an existing Maven project.

2. Enable Spring and Java Stack WS Support

2.1. Open the project's pom.xml
2.2. Insert the following <dependencies> section at the end of the XML file:
2.3. Insert the following <plugin> into the <build>/<plugins> section:
                        <!-- If you have a WSDL of your own, you may reference it here -->
2.4. Run the maven command "mvn clean install" to clean and build the application, as well as to generate the web service classes using wsdl2java.
2.5. In the generated sources folder, target/generated-sources, find the service endpoint interface class. It will be annotated with the @WebService annotation.
2.6. In the WSDL, find the service endpoint location. (It will be inside a <soap:address location=""/> element)
<wsdl:port name="WeatherForecastSoap12" binding="tns:WeatherForecastSoap12">
      <soap12:address location="" />
2.7. Create the Spring configuration file src/main/resources/META-INF/spring/applicationContext.xml and paste the following:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <context:component-scan base-package=""/>


The <context:component-scan> element allows Spring to automatically manage any Spring annotated beans under your base package.
The <stack-ws:consume> causes a client proxy to be generated for the supplied SOAP service endpoint interface (SEI). You may also supply "user" and "password" attributes if you need to authenticate using WS-Security.

3. Implement the Client Code

3.1. Create a new Java class at src/main/java/org/lds/example/soapclient/ and paste the following:
import java.text.MessageFormat;
import java.util.logging.Logger;

import javax.inject.Inject;

import net.webservicex.ArrayOfWeatherData;
import net.webservicex.WeatherData;
import net.webservicex.WeatherForecastSoap;
import net.webservicex.WeatherForecasts;

import org.springframework.stereotype.Component;

public class Client {

    private static final Logger LOG = LogUtils.getLogger();

    private WeatherForecastSoap endpoint;

    public void printWeatherForecast() {
        WeatherForecasts forecasts = endpoint.getWeatherByZipCode("84074");
        ArrayOfWeatherData weatherData = forecasts.getDetails();
        StringBuilder sb = new StringBuilder();
        for (WeatherData data : weatherData.getWeatherData()) {
            sb.append(MessageFormat.format("{0}: High: {1}, Low: {2}",
                    data.getDay(), data.getMaxTemperatureF(), data.getMinTemperatureC()));
The above code takes the injected service endpoint client proxy and invokes the getWeatherByZipCode method on it, afterwards printing the data from the response.
3.2. Open the file src/main/java/org/lds/example/soapclient/ and paste the following:
import org.springframework.context.ApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml");
        Client client = context.getBean(Client.class);

    private Main() {}
The above code initializes the Spring container from the Spring configuration XML file we provided. It then obtains the Spring-managed Client bean and invokes printWeatherForecast.

4. Run the Client

4.1. Run the main class from your IDE or (if outside the IDE) run the "install" goal on your project, then run the standalone executable from the command line as follows:
java -jar target/example-soap-client-1.0-bundle.jar
Disclaimer: The external web service used in this example is neither hosted nor maintained by the LDSTech community or the Java Stack team. It is intended as an example only and may fail or go away at any time.

« Back to Java Stack Training

This page was last modified on 15 July 2013, at 10:38.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.