9 Examples of Developing JAX-WS Web Service Clients
Each example provides step-by-step procedures for creating simple WebLogic web services and invoking an operation from a deployed web service. The examples include basic Java code and Ant build.xml
files that you can use in your own development environment to recreate the example, or by following the instructions to create and run the examples in an environment that is separate from your development environment.
The examples do not go into detail about the processes and tools used in the examples; later chapters are referenced for more detail.
Note:
For best practice examples demonstrating advanced web service features, see Roadmap for Developing JAX-WS Web Service Clients and Roadmap for Developing Reliable Web Services and Clients.
Parent topic: Developing Basic JAX-WS Web Service Clients
Developing a JAX-WS Java SE Client
Note:
You can invoke a web service from any Java SE or Jakarta EE application running on WebLogic Server (with access to the WebLogic Server classpath). Invoking a web service from standalone Java applications that are running in an environment where WebLogic Server libraries are not available is not supported in this release for JAX-WS web services.
When you invoke an operation of a deployed web service from a client application, the web service could be deployed to WebLogic Server or to any other application server, such as .NET. All you need to know is the URL to its public contract file, or WSDL.
In addition to writing the Java client application, you must also run the clientgen
WebLogic web service Ant task to generate the artifacts that your client application needs to invoke the web service operation. These artifacts include:
-
The Java class for the
Service
interface implementation for the particular web service you want to invoke. -
JAXB data binding artifacts.
-
The Java class for any user-defined XML Schema data types included in the WSDL file.
The following example shows how to create a Java client application that invokes the echoComplexType
operation of the ComplexService
WebLogic web service described in Creating a Web Service With User-Defined Data Types. The echoComplexType
operation takes as both a parameter and return type the BasicStruct
user-defined data type.
Note:
It is assumed in this procedure that you have created and deployed the ComplexService
web service.
You can use the build-client
and run
targets in the build.xml
file to iteratively update, rebuild, and run the Java client application as part of your development process.
Parent topic: Examples of Developing JAX-WS Web Service Clients
Sample Java Client Application
The following provides a simple Java client application that invokes the echoComplexType
operation. Because the <clientgen>
packageName
attribute was set to the same package name as the client application, we are not required to import the <clientgen>
-generated files.
package examples.webservices.simple_client; /** * This is a simple Java application that invokes the * echoComplexType operation of the ComplexService web service. */ public class Main { public static void main(String[] args) { ComplexService test = new ComplexService(); ComplexPortType port = test.getComplexPortTypePort(); BasicStruct in = new BasicStruct(); in.setIntValue(999); in.setStringValue("Hello Struct"); BasicStruct result = port.echoComplexType(in); System.out.println("echoComplexType called. Result: " + result.getIntValue() + ", " + result.getStringValue()); } }
Parent topic: Developing a JAX-WS Java SE Client
Sample Ant Build File For Building Java Client Application
The following build.xml
file defines tasks to build the Java client application. The example uses properties to simplify the file.
<project name="webservices-simple_client" default="all"> <!-- set global properties for this build --> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="example-output" value="output" /> <property name="clientclass-dir" value="${example-output}/clientclass" /> <path id="client.class.path"> <pathelement path="${clientclass-dir}"/> <pathelement path="${java.class.path}"/> </path> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <target name="clean" > <delete dir="${clientclass-dir}"/> </target> <target name="all" depends="clean,build-client,run" /> <target name="build-client"> <clientgen type="JAXWS" wsdl="http://${wls.hostname}:${wls.port}/complex/ComplexService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.simple_client"/> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java"/> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/simple_client/*.java"/> </target> <target name="run" > <java fork="true" classname="examples.webservices.simple_client.Main" failonerror="true" > <classpath refid="client.class.path"/> </java> </target> </project>
Parent topic: Developing a JAX-WS Java SE Client
Invoking a Web Service from a WebLogic Web Service
You can invoke a web service (WebLogic, Microsoft .NET, and so on) from within a deployed WebLogic web service.
The procedure is similar to that described in Developing a JAX-WS Java SE Client except that instead of running the clientgen
Ant task to generate the client stubs, you use the <clientgen>
child element of <jws>
, inside of the jwsc
Ant task. The jwsc
Ant task automatically packages the generated client stubs in the invoking web service WAR file so that the web service has immediate access to them. You then follow standard JAX-WS programming guidelines in the JWS file that implements the web service that invokes the other web service.
The following example shows how to write a JWS file that invokes the echoComplexType
operation of the ComplexService
web service described in Creating a Web Service With User-Defined Data Types.
Note:
It is assumed that you have successfully deployed the ComplexService
web service.
See Developing a JAX-WS Java SE Client for an example of creating a Java client application that invokes a web service.
Parent topic: Examples of Developing JAX-WS Web Service Clients
Sample ClientServiceImpl.java JWS File
The following provides a simple web service client application that invokes the echoComplexType
operation.
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; @WebService(name="ClientPortType", serviceName="ClientService", targetNamespace="http://examples.org") public class ClientServiceImpl { // Use the @WebServiceRef annotation to define a reference to the // ComplexService web service. @WebServiceRef() ComplexService test; @WebMethod() public String callComplexService(BasicStruct input, String serviceUrl) { // Create a port stub to invoke ComplexService ComplexPortType port = test.getComplexPortTypePort(); // Invoke the echoComplexType operation of ComplexService BasicStruct result = port.echoComplexType(input); System.out.println("Invoked ComplexPortType.echoComplexType." ); return "Invoke went okay! Here's the result: '" + result.getIntValue() + ", " + result.getStringValue() + "'"; } }
Parent topic: Invoking a Web Service from a WebLogic Web Service
Sample Ant Build File For Building ClientService
The following build.xml
file defines tasks to build the client application. The example uses properties to simplify the file.
The following build.xml
file uses properties to simplify the file.
<project name="webservices-service_to_service" default="all"> <!-- set global properties for this build --> <property name="wls.username" value="weblogic" /> <property name="wls.password" value="weblogic" /> <property name="wls.hostname" value="localhost" /> <property name="wls.port" value="7001" /> <property name="wls.server.name" value="myserver" /> <property name="ear.deployed.name" value="ClientServiceEar" /> <property name="example-output" value="output" /> <property name="ear-dir" value="${example-output}/ClientServiceEar" /> <property name="clientclass-dir" value="${example-output}/clientclasses" /> <path id="client.class.path"> <pathelement path="${clientclass-dir}"/> <pathelement path="${java.class.path}"/> </path> <taskdef name="jwsc" classname="weblogic.wsee.tools.anttasks.JwscTask" /> <taskdef name="clientgen" classname="weblogic.wsee.tools.anttasks.ClientGenTask" /> <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"/> <target name="all" depends="clean,build-service,deploy,client" /> <target name="clean" depends="undeploy"> <delete dir="${example-output}"/> </target> <target name="build-service"> <jwsc srcdir="src" destdir="${ear-dir}" > <jws file="examples/webservices/service_to_service/ClientServiceImpl.java" type="JAXWS"> <WLHttpTransport contextPath="ClientService" serviceUri="ClientService" portName="ClientServicePort"/> <clientgen type="JAXWS" wsdl="http://${wls.hostname}:${wls.port}/complex/ComplexService?WSDL" packageName="examples.webservices.complex" /> </jws> </jwsc> </target> <target name="deploy"> <wldeploy action="deploy" name="${ear.deployed.name}" source="${ear-dir}" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="undeploy"> <wldeploy action="undeploy" name="${ear.deployed.name}" failonerror="false" user="${wls.username}" password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}" /> </target> <target name="client"> <clientgen wsdl="http://${wls.hostname}:${wls.port}/ClientService/ClientService?WSDL" destDir="${clientclass-dir}" packageName="examples.webservices.service_to_service.client" type="JAXWS"/> <javac srcdir="${clientclass-dir}" destdir="${clientclass-dir}" includes="**/*.java"/> <javac srcdir="src" destdir="${clientclass-dir}" includes="examples/webservices/service_to_service/client/**/*.java"/> </target> <target name="run"> <java classname="examples.webservices.service_to_service.client.Main" fork="true" failonerror="true" > <classpath refid="client.class.path"/> </java> </target> </project>
Parent topic: Invoking a Web Service from a WebLogic Web Service