Web Service Clients
This section shows how to create and run these types of clients:
When you run these client examples, they will access the
MyHelloServicethat you deployed in Creating a Simple Web Service and Client with JAX-RPC.Dynamic Proxy Client
This example resides in the
<INSTALL>/j2eetutorial14/examples/jaxrpc/dynamicproxy/directory.The client in the preceding section uses a static stub for the proxy. In contrast, the client example in this section calls a remote procedure through a dynamic proxy, a class that is created during runtime. Although the source code for the static stub client relies on an implementation-specific class, the code for the dynamic proxy client does not have this limitation.
Coding the Dynamic Proxy Client
The
DynamicProxyHelloprogram constructs the dynamic proxy as follows:
- Creates a
Serviceobject namedhelloService:
Service helloService =
serviceFactory.createService(helloWsdlUrl,
new QName(nameSpaceUri, serviceName));A
Serviceobject is a factory for proxies. To create theServiceobject (helloService), the program calls thecreateServicemethod on another type of factory, aServiceFactoryobject.The
createServicemethod has two parameters: the URL of the WSDL file and aQNameobject. At runtime, the client gets information about the service by looking up its WSDL. In this example, the URL of the WSDL file points to the WSDL that was deployed withMyHelloService:
http://localhost:8080/hello-jaxrpc/hello?WSDLA
QNameobject is a tuple that represents an XML qualified name. The tuple is composed of a namespace URI and the local part of the qualified name. In theQNameparameter of thecreateServiceinvocation, the local part is the service name,MyHelloService.- The program creates a proxy (
myProxy) with a type of the service endpoint interface (HelloIF):d
ynamicproxy.HelloIF myProxy =
(dynamicproxy.HelloIF)helloService.getPort(
new QName(nameSpaceUri, portName),
dynamicproxy.HelloIF.class);The
helloServiceobject is a factory for dynamic proxies. To createmyProxy, the program calls thegetPortmethod ofhelloService. This method has two parameters: aQNameobject that specifies the port name and ajava.lang.Classobject for the service endpoint interface (HelloIF). TheHelloIFclass is generated bywscompile. The port name (HelloIFPort) is specified by the WSDL file.Here is the listing for the
HelloClient.javafile, located in the<INSTALL>/j2eetutorial14/examples/jaxrpc/dynamicproxy/src/directory:package dynamicproxy; import java.net.URL; import javax.xml.rpc.Service; import javax.xml.rpc.JAXRPCException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceFactory; import dynamicproxy.HelloIF; public class HelloClient { public static void main(String[] args) { try { String UrlString = args[0] + "?WSDL"; String nameSpaceUri = "urn:Foo"; String serviceName = "MyHelloService"; String portName = "HelloIFPort"; System.out.println("UrlString = " + UrlString); URL helloWsdlUrl = new URL(UrlString); ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName)); dynamicproxy.HelloIF myProxy = (dynamicproxy.HelloIF) helloService.getPort( new QName(nameSpaceUri, portName), dynamicproxy.HelloIF.class); System.out.println(myProxy.sayHello("Buzz")); } catch (Exception ex) { ex.printStackTrace(); } } }Building and Running the Dynamic Proxy Client
Before performing the steps in this section, you must first create and deploy
MyHelloServiceas described in Creating a Simple Web Service and Client with JAX-RPC.To build and package the client, go to the
<INSTALL>/j2eetutorial14/examples/jaxrpc/dynamicproxy/directory and type the following:The preceding command runs these tasks:
The
generate-interfacetask runswscompilewith the-importoption. Thewscompilecommand reads theMyHelloService.wsdlfile and generates the service endpoint interface class (HelloIF.class). Although thiswscompileinvocation also creates stubs, the dynamic proxy client does not use these stubs, which are required only by static stub clients.The c
ompile-clienttask compiles thesrc/HelloClient.javafile.The
package-dynamictask creates thedist/client.jarfile, which containsHelloIF.classandHelloClient.class.To run the client, type the following:
The client should display the following line:
Dynamic Invocation Interface Client
This example resides in the
<INSTALL>/j2eetutorial14/examples/jaxrpc/dii/directory.With the dynamic invocation interface (DII), a client can call a remote procedure even if the signature of the remote procedure or the name of the service is unknown until runtime. In contrast to a static stub or dynamic proxy client, a DII client does not require runtime classes generated by
wscompile. However, as you'll see in the following section, the source code for a DII client is more complicated than the code for the other two types of clients.This example is for advanced users who are familiar with WSDL documents. (See Further Information.)
Coding the DII Client
The
DIIHelloprogram performs these steps:
- Creates a
Serviceobject:
Service service =
factory.createService(new QName(qnameService));To get a
Serviceobject, the program invokes thecreateServicemethod of aServiceFactoryobject. The parameter of thecreateServicemethod is aQNameobject that represents the name of the service,MyHelloService. The WSDL file specifies this name as follows:
<service name="MyHelloService">- From the
Serviceobject, creates aCallobject:
QName port = new QName(qnamePort);
Call call = service.createCall(port);A
Callobject supports the dynamic invocation of the remote procedures of a service. To get aCallobject, the program invokes theServiceobject'screateCallmethod. The parameter ofcreateCallis aQNameobject that represents the service endpoint interface,MyHelloServiceRPC. In the WSDL file, the name of this interface is designated by theportTypeelement:
<portType name="HelloIF">- Sets the service endpoint address on the
Callobject:
call.setTargetEndpointAddress(endpoint);In the WSDL file, this address is specified by the
<soap:address>element.- Sets these properties on the
Callobject:
SOAPACTION_USE_PROPERTY
SOAPACTION_URI_PROPERTY
ENCODING_STYLE_PROPERTYTo learn more about these properties, refer to the SOAP and WSDL documents listed in Further Information.
- Specifies the method's return type, name, and parameter:
QName QNAME_TYPE_STRING = new QName(NS_XSD, "string");
call.setReturnType(QNAME_TYPE_STRING);
call.setOperationName(new QName(BODY_NAMESPACE_VALUE,
"sayHello"));
call.addParameter("String_1", QNAME_TYPE_STRING,
ParameterMode.IN);To specify the return type, the program invokes the
setReturnTypemethod on theCallobject. The parameter ofsetReturnTypeis aQNameobject that represents an XML string type.The program designates the method name by invoking the
setOperationNamemethod with aQNameobject that representssayHello.To indicate the method parameter, the program invokes the
addParametermethod on theCallobject. TheaddParametermethod has three arguments: aStringfor the parameter name (String_1), aQNameobject for the XML type, and aParameterModeobject to indicate the passing mode of the parameter (IN).- Invokes the remote method on the
Callobject:
String[] params = { "Murphy" };
String result = (String)call.invoke(params);The program assigns the parameter value (
Murphy) to aStringarray (params) and then executes theinvokemethod with theStringarray as an argument.Here is the listing for the
HelloClient.javafile, located in the<INSTALL>/j2eetutorial14/examples/jaxrpc/dii/src/directory:package dii; import javax.xml.rpc.Call; import javax.xml.rpc.Service; import javax.xml.rpc.JAXRPCException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceFactory; import javax.xml.rpc.ParameterMode; public class HelloClient { private static String qnameService = "MyHelloService"; private static String qnamePort = "HelloIF"; private static String BODY_NAMESPACE_VALUE = "urn:Foo"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; private static String NS_XSD = "http://www.w3.org/2001/XMLSchema"; private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/"; public static void main(String[] args) { System.out.println("Endpoint address = " + args[0]); try { ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService( new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setTargetEndpointAddress(args[0]); call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY ""); call.setProperty(ENCODING_STYLE_PROPERTY, URI_ENCODING); QName QNAME_TYPE_STRING = new QName(NS_XSD, "string"); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName( new QName(BODY_NAMESPACE_VALUE,"sayHello")); call.addParameter("String_1", QNAME_TYPE_STRING, ParameterMode.IN); String[] params = { "Murph!" }; String result = (String)call.invoke(params); System.out.println(result); } catch (Exception ex) { ex.printStackTrace(); } } }Building and Running the DII Client
Before performing the steps in this section, you must first create and deploy
MyHelloServiceas described in Creating a Simple Web Service and Client with JAX-RPC.To build and package the client, go to the
<INSTALL>/j2eetutorial14/examples/jaxrpc/dii/directory and type the following:This
buildtask compilesHelloClientand packages it into thedist/client.jarfile. Unlike the previous client examples, the DII client does not require files generated bywscompile.To run the client, type this command:
The client should display this line:
Application Client
Unlike the stand-alone clients in the preceding sections, the client in this section is an application client. Because it's a J2EE component, an application client can locate a local web service by invoking the JNDI
lookupmethod.J2EE Application HelloClient Listing
Here is the listing for the
HelloClient.javafile, located in the<INSTALL>/j2eetutorial14/examples/jaxrpc/appclient/src/directory:package appclient; import javax.xml.rpc.Stub; import javax.naming.*; public class HelloClient { private String endpointAddress; public static void main(String[] args) { System.out.println("Endpoint address = " + args[0]); try { Context ic = new InitialContext(); MyHelloService myHelloService = (MyHelloService) ic.lookup("java:comp/env/service/MyJAXRPCHello"); appclient.HelloIF helloPort = myHelloService.getHelloIFPort(); ((Stub)helloPort)._setProperty (Stub.ENDPOINT_ADDRESS_PROPERTY,args[0]); System.out.println(helloPort.sayHello("Jake!")); System.exit(0); } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } } }Building the Application Client
Before performing the steps in this section, you must first create and deploy
MyHelloServiceas described in Creating a Simple Web Service and Client with JAX-RPC.To build the client, go to the
<INSTALL>/j2eetutorial14/examples/jaxrpc/appclient/directory and type the following:As with the static stub client, the preceding command compiles
HelloClient.javaand runswscompileby invoking thegenerate-stubstarget.Packaging the Application Client
Packaging this client is a two-step process:
To create the EAR file, follow these steps:
To start the New Application Client wizard, select File
New
Application Client. The wizard displays the following dialog boxes.
- Introduction dialog box
- JAR File Contents dialog box
- Select the button labeled Create New AppClient Module in Application.
- In the combo box below this button, select
HelloServiceApp.- In the AppClient Display Name field, enter
HelloClient.- Click Edit Contents.
- In the tree under Available Files, locate the
<INSTALL>/j2eetutorial14/examples/jaxrpc/appclientdirectory.- Select the
builddirectory.- Click Add.
- Click OK.
- Click Next.
- General dialog box
Specifying the Web Reference
When it invokes the
lookupmethod, theHelloClientrefers to the web service as follows:You specify this reference as follows.
- In the tree, select
HelloClient.- Select the Web Service Refs tab.
- Click Add.
- In the Coded Name field, enter
service/MyJAXRPCHello.- In the Service Interface combo box, select
appclient.MyHelloService.- In the WSDL File combo box, select
META-INF/wsdl/MyHelloService.wsdl.- In the Namespace field, enter
urn:Foo.- In the Local Part field, enter
MyHelloService.- In the Mapping File combo box, select
mapping.xml.- Click OK.
Deploying and Running the Application Client
To deploy the application client, follow these steps:
To run the client follow these steps:
The client should display this line:
More JAX-RPC Clients
Other chapters in this book also have JAX-RPC client examples:
- Chapter 16 shows how a JSP page can be a static stub client that accesses a remote web service. See The Example JSP Pages.
- Chapter 32 includes a static stub client that demonstrates basic authentication. See Example: Basic Authentication with JAX-RPC.
- Chapter 32 includes a static stub client that demonstrates mutual authentication. See Example: Client-Certificate Authentication over HTTP/SSL with JAX-RPC.