This section describes how to write the code for a simple Java client, based on the WSDL contract from Example 3.1, “HelloWorld WSDL Contract”. To implement the consumer, you need to use the following stubs:
Service class (that is, SOAPService
).
SEI (that is, Greeter
).
Example 5.1, “Outline of a Generated Service Class” shows the typical outline a generated service class, ServiceName
, which extends the javax.xml.ws.Service
base class.
Example 5.1. Outline of a Generated Service Class
@WebServiceClient(name="..." targetNamespace="..." wsdlLocation="...") public class ServiceName extends javax.xml.ws.Service { ... public ServiceName(URL wsdlLocation, QName serviceName) { } public ServiceName() { } @WebEndpoint(name="SoapPort") public Greeter getPortName() { } . . . }
The ServiceName
class in Example 5.1, “Outline of a Generated Service Class” defines the following methods:
Constructor methods — the following forms of constructor are defined:
constructs a service object based on the data in the ServiceName
(URL wsdlLocation, QName serviceName)serviceName
service in the WSDL contract that is obtainable from wsdlLocation
.
is the default constructor, which constructs a service object based on the service name and WSDL contract that were provided at the time the stub code was generated (for example, when running wsdl2java). Using this constructor presupposes that the WSDL contract remains available at its original location.ServiceName
()
get
methods — for every PortName
()PortName
port defined on the ServiceName
service, wsdl2java generates a corresponding get
method in Java. Therefore, a PortName
()wsdl:service
element that defines multiple endpoints will generate a service class with multiple get
methods.PortName
()
For every port type defined in the original WSDL contract, you can generate a corresponding SEI. A service endpoint interface is the Java mapping of a wsdl:portType
element. Each operation defined in the original wsdl:portType
element maps to a corresponding method in the SEI. The operation's parameters are mapped as follows:
The input parameters are mapped to method arguments.
The first output parameter is mapped to a return value.
If there is more than one output parameter, the second and subsequent output parameters map to method arguments (moreover, the values of these arguments must be passed using Holder types).
For example, Example 5.2, “The Greeter Service Endpoint Interface” shows the Greeter SEI, which is generated from the wsdl:portType
element defined in Example 3.1, “HelloWorld WSDL Contract”. For simplicity, Example 5.2, “The Greeter Service Endpoint Interface” omits the standard JAXB and JAX-WS annotations.
Example 5.2. The Greeter Service Endpoint Interface
/* Generated by WSDLToJava Compiler. */ package org.apache.hello_world_soap_http; ... public interface Greeter { public String sayHi(); public String greetMe(String requestType); public void greetMeOneWay(String requestType); public void pingMe() throws PingMeFault; }
Example 5.3, “Consumer Implementation Code” shows the code that implements the HelloWorld consumer. In summary, the consumer connects to the SoapPort port on the SOAPService service and then proceeds to invoke each of the operations supported by the Greeter port type.
Example 5.3. Consumer Implementation Code
package demo.hw.client; import java.io.File; import java.net.URL; import javax.xml.namespace.QName; import org.apache.hello_world_soap_http.Greeter; import org.apache.hello_world_soap_http.PingMeFault; import org.apache.hello_world_soap_http.SOAPService; public final class Client { private static final QName SERVICE_NAME = new QName("http://apache.org/hello_world_soap_http", "SOAPService"); private Client() { } public static void main(String args[]) throws Exception { if (args.length == 0) { System.out.println("please specify wsdl"); System.exit(1); } URL wsdlURL; File wsdlFile = new File(args[0]); if (wsdlFile.exists()) { wsdlURL = wsdlFile.toURL(); } else { wsdlURL = new URL(args[0]); } System.out.println(wsdlURL); SOAPService ss = new SOAPService(wsdlURL,SERVICE_NAME); Greeter port = ss.getSoapPort(); String resp; System.out.println("Invoking sayHi..."); resp = port.sayHi(); System.out.println("Server responded with: " + resp); System.out.println(); System.out.println("Invoking greetMe..."); resp = port.greetMe(System.getProperty("user.name")); System.out.println("Server responded with: " + resp); System.out.println(); System.out.println("Invoking greetMeOneWay..."); port.greetMeOneWay(System.getProperty("user.name")); System.out.println("No response from server as method is OneWay"); System.out.println(); try { System.out.println("Invoking pingMe, expecting exception..."); port.pingMe(); } catch (PingMeFault ex) { System.out.println("Expected exception: PingMeFault has occurred."); System.out.println(ex.toString()); } System.exit(0); } }
The Client.main()
method from Example 5.3, “Consumer Implementation Code” proceeds as follows: