6-6 Getting Started With JAX-RPC Web Services for Oracle WebLogic Server
Web service. The name of the Web service is contained in the service element, as shown in the following excerpt of the TraderService WSDL:
service name=TraderService port name=TraderServicePort
binding=tns:TraderServiceSoapBinding ...
port service
The operations defined for this Web service are listed under the corresponding binding
element. For example, the following WSDL excerpt shows that the TraderService
Web service has two operations, buy and sell for clarity, only relevant parts of the WSDL are shown:
binding name=TraderServiceSoapBinding ... ...
operation name=sell ...
operation operation name=buy
operation binding
6.2.3 Writing the Java Client Application Code to Invoke a Web Service
In the following code example, a Java application invokes a Web service operation. The client application takes a single argument: the WSDL of the Web service.The
application then uses standard JAX-RPC API code and the Web service-specific implementation of the Service interface, generated by clientgen, to invoke an
operation of the Web service.
The example also shows how to invoke an operation that has a user-defined data type examples.webservices.complex.BasicStruct as an input parameter and
return value. The clientgen Ant task automatically generates the Java code for this user-defined data type.
package examples.webservices.simple_client; import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException; import the BasicStruct class, used as a param and return value of the
echoComplexType operation. The class is generated automatically by the clientgen Ant task.
import examples.webservices.complex.BasicStruct; This is a simple Java client application that invokes the
the echoComplexType operation of the ComplexService Web service. public class Main {
public static void mainString[] args throws ServiceException, RemoteException {
ComplexService service = new ComplexService_Impl args[0] + ?WSDL ; ComplexPortType port = service.getComplexServicePort;
BasicStruct in = new BasicStruct; in.setIntValue999;
in.setStringValueHello Struct; BasicStruct result = port.echoComplexTypein;
System.out.printlnechoComplexType called. Result: + result.getIntValue
Invoking Web Services 6-7
+ , + result.getStringValue; }
}
In the preceding example:
■
The following code shows how to create a ComplexPortType stub: ComplexService service = new ComplexService_Impl args[0] + ?WSDL;
ComplexPortType port = service.getComplexServicePort; The ComplexService_Impl stub factory implements the JAX-RPC Service
interface. The constructor of ComplexService_Impl creates a stub based on the provided WSDL URI args[0] + ?WSDL. The
getComplexServicePort method is used to return an instance of the
ComplexPortType stub implementation.
■
The following code shows how to invoke the echoComplexType operation of the ComplexService
Web service: BasicStruct result = port.echoComplexTypein;
The echoComplexType operation returns the user-defined data type called BasicStruct
. The method of your application that invokes the Web service operation must throw or
catch java.rmi.RemoteException and javax.xml.rpc.ServiceException, both of which are thrown from the generated JAX-RPC stubs.
6.2.4 Compiling and Running the Client Application