Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


1. Overview

In this tutorial, we’ll learn how to build a SOAP client in Java with JAX-WS RI in Java 8 and 11.

First, we’ll generate the client code using the wsimport utility and then test it using a JUnit.

For those starting out, our introduction to JAX-WS provides great background on the subject.

2. The Web Service

Before we start building a client, we need a server. In this case, we need a server exposing a JAX-WS web service.

For the purpose of this tutorial, we’ll use a web service that will fetch us a country’s data, given its name.

2.1. Summary of Implementation

Since we’re focusing on building the client, we won’t get into the implementation details of our service.

Let’s say that an interface CountryService is used to expose the web service to the external world. To keep things simple, we’ll build and deploy the web service using the API in our class CountryServicePublisher.

We’ll run CountryServicePublisher as a Java application to publish an endpoint that’ll accept the incoming requests. In other words, this will be our server.

After starting the server, hitting the URL http://localhost:8888/ws/country?wsdl gives us the web service description file. The WSDL acts as a guide to understand the service’s offerings and generate implementation code for the client.

2.2. The Web Services Description Language

Let’s look at our web service’s WSDL, country:

<?xml version="1.0" encoding="UTF-8"?>
<definitions <!-- namespace declarations -->
    targetNamespace="" name="CountryServiceImplService">
            <xsd:import namespace="" 
    <message name="findByName">
        <part name="arg0" type="xsd:string"></part>
    <message name="findByNameResponse">
        <part name="return" type="tns:country"></part>
    <portType name="CountryService">
        <operation name="findByName">
            <input wsam:Action="" 
            <output wsam:Action="" 
    <binding name="CountryServiceImplPortBinding" type="tns:CountryService">
        <soap:binding transport="" style="rpc"></soap:binding>
        <operation name="findByName">
            <soap:operation soapAction=""></soap:operation>
                <soap:body use="literal" namespace=""></soap:body>
                <soap:body use="literal" namespace=""></soap:body>
    <service name="CountryServiceImplService">
        <port name="CountryServiceImplPort" binding="tns:CountryServiceImplPortBinding">
            <soap:address location="http://localhost:8888/ws/country"></soap:address>

In a nutshell, this is the useful information it provides:

  • We can invoke the method findByName with a string argument.
  • In response, the service will return us a custom type of country.
  • Types are defined in an xsd schema generated at the location http://localhost:8888/ws/country?xsd=1:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema <!-- namespace declarations -->
    <xs:complexType name="country">
            <xs:element name="capital" type="xs:string" minOccurs="0"></xs:element>
            <xs:element name="currency" type="tns:currency" minOccurs="0"></xs:element>
            <xs:element name="name" type="xs:string" minOccurs="0"></xs:element>
            <xs:element name="population" type="xs:int"></xs:element>
    <xs:simpleType name="currency">
        <xs:restriction base="xs:string">
            <xs:enumeration value="EUR"></xs:enumeration>
            <xs:enumeration value="INR"></xs:enumeration>
            <xs:enumeration value="USD"></xs:enumeration>

That’s all we need to implement a client.

Let’s see how in the next section.

3. Using wsimport to Generate Client Code

3.1. For JDK 8

First, let’s see how to generate client code using JDK 8.

To begin with, let’s add a plugin to our pom.xml to use this tool via Maven:


Second, let’s execute this plugin:

mvn clean jaxws:wsimport

That’s all! The above command will generate code in the specified package inside the sourceDestDir we provided in the plugin configuration.

Another way to achieve the same would be to use the wsimport utility. It comes out of the box with the standard JDK 8 distribution and can be found under JAVA_HOME/bin directory.

To generate client code using wsimport, we can navigate to the project’s root and run this command:

JAVA_HOME/bin/wsimport -s src/main/java/ -keep -p "http://localhost:8888/ws/country?wsdl"

It’s important to bear in mind that the service endpoint should be available in order to successfully execute the plugin or command.

3.2. For JDK 11

Starting JDK 11, wsimport was removed as part of the JDK and no longer comes out of the box with the standard distribution.

However, it was open-sourced to the Eclipse foundation.

In order to use wsimport to generate client code for Java 11 and above, we need to add the, jaxws-rt and jaxws-ri dependencies in addition to the jaxws-maven-plugin:


Now, to generate the client code in the package, we’ll need the same Maven command as before:

mvn clean jaxws:wsimport

Next, let’s look at the generated artifacts that are the same for both the Java versions.

3.3. Generated POJOs

Based on the xsd we saw earlier, the tool will generate a file named

@XmlType(name = "country", propOrder = { "capital", "currency", "name", "population" })
public class Country {
    protected String capital;
    @XmlSchemaType(name = "string")
    protected Currency currency;
    protected String name;
    protected int population;
    // standard getters and setters

As we can see, the generated class is decorated with JAXB annotations for marshalling and unmarshalling the object to and from XML.

Also, it generates a Currency enum:

@XmlType(name = "currency")
public enum Currency {
    EUR, INR, USD;
    public String value() {
        return name();
    public static Currency fromValue(String v) {
        return valueOf(v);

3.4. CountryService

The second generated artifact is an interface that acts as a proxy to the actual web service.

The interface CountryService declares the same method as our server, findByName:

@WebService(name = "CountryService", targetNamespace = "")
@SOAPBinding(style = SOAPBinding.Style.RPC)
@XmlSeeAlso({ ObjectFactory.class })
public interface CountryService {
    @WebResult(partName = "return")
    @Action(input = "", 
      output = "")
    public Country findByName(@WebParam(name = "arg0", partName = "arg0") String arg0);

Notably, the interface is marked as a javax.jws.WebService, with a SOAPBinding.Style as RPC as defined by the service’s WSDL.

The method findByName is annotated to declare that it’s a javax.jws.WebMethod, with its expected input and output parameter types.

3.5. CountryServiceImplService

Our next generated class, CountryServiceImplService, extends

Its annotation WebServiceClient denotes that it is the client view of a service:

@WebServiceClient(name = "CountryServiceImplService", 
  targetNamespace = "", 
  wsdlLocation = "http://localhost:8888/ws/country?wsdl")
public class CountryServiceImplService extends Service {

    private final static WebServiceException COUNTRYSERVICEIMPLSERVICE_EXCEPTION;
    private final static QName COUNTRYSERVICEIMPLSERVICE_QNAME = 
      new QName("", "CountryServiceImplService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8888/ws/country?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);

    public CountryServiceImplService() {
        super(__getWsdlLocation(), COUNTRYSERVICEIMPLSERVICE_QNAME);

    // other constructors 

    @WebEndpoint(name = "CountryServiceImplPort")
    public CountryService getCountryServiceImplPort() {
        return super.getPort(new QName("", "CountryServiceImplPort"), 

    private static URL __getWsdlLocation() {


The important method to note here is getCountryServiceImplPort. Given a qualified name of the service endpoint, or QName, and the dynamic proxy’s service endpoint interface name, it returns a proxy instance.

To invoke the web service, we need to use this proxy, as we’ll see shortly.

Using a proxy makes it seem as if we are calling a service locally, abstracting away the intricacies of remote invocation.

4. Testing the Client

Next, we’ll write a JUnit test to connect to the web service using the generated client code.

Before we can do that, we need to get the service’s proxy instance at the client end:

public static void setup() {
    CountryServiceImplService service = new CountryServiceImplService();
    CountryService countryService = service.getCountryServiceImplPort();

For more advanced scenarios such as enabling or disabling a WebServiceFeature, we can use other generated constructors for CountryServiceImplService.

Now let’s look at some tests:

public void givenCountryService_whenCountryIndia_thenCapitalIsNewDelhi() {
    assertEquals("New Delhi", countryService.findByName("India").getCapital());

public void givenCountryService_whenCountryFrance_thenPopulationCorrect() {
    assertEquals(66710000, countryService.findByName("France").getPopulation());

public void givenCountryService_whenCountryUSA_thenCurrencyUSD() {
    assertEquals(Currency.USD, countryService.findByName("USA").getCurrency());

As we can see, invoking the remote service’s methods became as simple as calling methods locally. The proxy’s findByName method returned a Country instance matching the name we provided. Then we used various getters of the POJO to assert expected values.

5. Conclusion

In this article, we saw how to invoke a SOAP web service in Java using JAX-WS RI and the wsimport utility for Java 8 as well as 11.

Alternatively, we can use other JAX-WS implementations such as Apache CXF, Apache Axis2 and Spring to do the same.

As always, the source code is available over on GitHub for both JDK 8 and JDK 11 versions.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


res – REST with Spring (eBook) (everywhere)
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.