Web service client given WSDL

As hinted in a comment to Jon’s answer, my recommendation would be to use a JAX-WS implementation like JAX-WS RI (which is included in Java 6) or Apache CXF.

I’ll use JAX-WS RI to illustrate my answer as it’s available out of the box, on the command line (to explain the steps) but I’d recommend using an IDE with good JAX-WS support e.g. NetBeans (see the resources at the end of the answer).

1. Generate JAX-WS artifacts from the WSDL

First run wsimport to generate JAX-WS artifacts (to put it simply, the classes you’ll need to invoke the web service):

wsimport -d generated -extension -keep -p com.gatewayedi.ws -XadditionalHeaders https://testservices.gatewayedi.com/PayerList/payerlist.asmx?wsdl 

About the options:

  • -d is used to specify the target directory for the generated stuff
  • -extension is used to allow extensions (the WSDL is using a non-standard SOAP 1.2 binding)
  • -keep is to keep generated .java sources (this is will ease the development)
  • -p is used to specify a package for the generated artifacts
  • -XadditionalHeaders is used to map additional WSDL headers (that are not part of the input or output contract defined in the portType operation) to method parameters (this will make invoking the service easier).

Sorry if some of the vocabulary is cryptic but, well, welcome to SOAP web services 🙂

2. Implement a client

Here is a simple client showing how to invoke one of the available operations using the generated classes:

import com.gatewayedi.ws.AuthSOAPHeader;
import com.gatewayedi.ws.PayerList;
import com.gatewayedi.ws.PayerListSoap;

public class Main {

    public static void main(String[] args) {
        new Main().callWebService();
    }

    private void callWebService() {
        PayerList service = new PayerList();
        PayerListSoap port = service.getPayerListSoap();

        AuthSOAPHeader authSOAPHeader = new AuthSOAPHeader();
        authSOAPHeader.setUser("test");
        authSOAPHeader.setPassword("test");

        String payerList = port.ping(authSOAPHeader);

        System.out.println(payerList);
    }

}

Below, the generated request:

<?xml version="1.0"  standalone="no"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<AuthSOAPHeader xmlns="https://ws.gatewayedi.com/">
<User>test</User>
<Password>test</Password>
</AuthSOAPHeader>
</S:Header>
<S:Body>
<Ping xmlns="https://ws.gatewayedi.com/"/>
</S:Body>
</S:Envelope>

Don’t know what credentials you’re supposed to pass though.

Resources

  • Developing JAX-WS Web Service Clients (start here)
  • Introducing JAX-WS 2.0 With the Java SE 6 Platform, Part 1
  • Creating a Simple Web Service and Client with JAX-WS
  • Creating a SOAP client with either Apache CXF or GlassFish Metro (Glen Mazza’s blog is a great resources)

Related questions

  • Java Webservice Client (Best way)
  • What is the best java webservice framework?

Leave a Comment