Sample JWS File That Invokes a Web Service

Invoking Web Services 6-11

6.3.2 Sample JWS File That Invokes a Web Service

The following sample JWS file, called ClientServiceImpl.java, implements a Web service called ClientService that has an operation that in turn invokes the echoComplexType operation of a Web service called ComplexService. This operation has a user-defined data type BasicStruct as both a parameter and a return value. The relevant code is shown in bold and described after the example. package examples.webservices.service_to_service; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.WebServiceRef; Import the BasicStruct data type, generated by clientgen and used by the ComplexService Web Service import examples.webservices.complex.BasicStruct; Import the JAX-WS stubs generated by clientgen for invoking the ComplexService Web service. import examples.webservices.complex.ComplexPortType; import examples.webservices.complex.ComplexService; WebServicename=ClientPortType, serviceName=ClientService, targetNamespace=http:examples.org public class ClientServiceImpl { Use the WebServiceRef annotation to define a reference to a Web service. WebServiceRef ComplexService test; WebMethod public String callComplexServiceBasicStruct input, String serviceUrl { Create a port stub to invoke ComplexService ComplexPortType port = test.getComplexPortTypePort; Invoke the echoComplexType operation of ComplexService BasicStruct result = port.echoComplexTypeinput; System.out.printlnInvoked ComplexPortType.echoComplexType. ; return Invoke went okay Heres the result: + result.getIntValue + , + result.getStringValue + ; } } Follow these guidelines when programming the JWS file that invokes another Web service; code snippets of the guidelines are shown in bold in the preceding example: ■ Import any user-defined data types that are used by the invoked Web service. In this example, the ComplexService uses the BasicStruct JavaBean: import examples.webservices.complex.BasicStruct; ■ Import the JAX-WS interfaces of the ComplexService Web service; the stubs are generated by the cliengen child element of jws: import examples.webservices.complex.ComplexPortType; import examples.webservices.complex.ComplexService; ■ Define a reference to a Web service and an injection target for it using the WebServiceRef annotation: 6-12 Getting Started With JAX-WS Web Services for Oracle WebLogic Server WebServiceRef ComplexService service; Alternatively, you can create a proxy stub to the ComplexService Web service, as shown below: ComplexService test = new ComplexService; ■ Return an instance of the ComplexPortType stub implementation by calling the getComplexPortTypePort operation on the Web service reference: ComplexPortType port = service.getComplexPortTypePort; ■ Invoke the echoComplexType operation of ComplexService using the port you just instantiated: BasicStruct result = port.echoComplexTypeinput;

6.4 Configuring Web Service Clients