Writing a Java-based Web Service Client
The project directory (
src_client) and the example Java source file
src_client/org/myorg/MathClient.java were created in the step
Create a Project.
The MathClient.java source file is dependent upon source artifacts, which are generated from your deployed web service. Before actually being able to complete your client source, you must generate and compile these artifacts. As generated, MathClient.java compiles, but does nothing until you use it to invoke a web service method.
In order to get these artifacts, you must compile your empty client using the following commands:
% cd <Windchill>/prog_examples/jws/MyProject/src_client
% ant
The Apache Ant script also generates an executable JAR file of your application:
<Windchill>/prog_examples/jws/MyProject/dist_client/MathServiceClient.jar
However, since your main class does nothing, then executing the JAR also does nothing. Running the previous ant command processes the WSDL for your web service and generates Java source artifacts to the following:
<Windchill>/prog_examples/jws/MyProject/gensrc_client
This creates Java source code within the com.ptc.jws.service.org.myorg.mathservice package. To avoid Java code being generated into a package with a name derived from the service name space, you can use the jaxws.package property within the build.xml file to explicitly define which package you want the source code generated into.
Edit the client source code to complete your client:
1. In a text editor, open the following file:
<Windchill>/prog_examples/jws/MyProject/src_client/org/myorg/MathClient.java
The source looks something like:
package org.myorg;
// import the classes generated when compiling your client
//import com.ptc.jws.service.?.*;
import com.ptc.jws.client.handler.*;
public class MathClient
{
public static void main ( String [] args ) throws java.lang.Exception
{
// depending on your security requirements you may need to specify credentials up
// front, or on the command-line where most policies will prompt for them
//Credentials.setUsername ( "demo" );
//Credentials.setPassword ( "demo" );
// TODO implement your client /*
MathServiceImplService service = new MathServiceImplService();
MathServiceImpl port = service.getMathServiceImplPort ();
//invoke an action
//port.methodName ( <arguments> );
*/
}
}
2. Update the source to import the server-side web service artifacts, and invoke a method as follows:
package org.myorg;
// import the classes generated when compiling your client
import com.ptc.jws.service.org.myorg.mathservice.*;
public class MathClient
{
public static void main ( String [] args ) throws java.lang.Exception
{
int a = args.length > 0 ? Integer.parseInt ( args[0] ) : 0;
int b = args.length > 1 ? Integer.parseInt ( args[1] ) : 0;
MathServiceImplService service = new MathServiceImplService();
MathServiceImpl port = service.getMathServiceImplPort ();
System.out.printf ( "a+b=%d\n", port.add ( a, b ) );
System.out.printf ( "a-b=%d\n", port.subtract ( a, b ) );
System.out.printf ( "a*b=%d\n", port.multiply ( a, b ) );
System.out.printf ( "a/b=%f\n", port.divide ( a, b ) );
}
}
3. Recompile your client and update your executable JAR to include the updated classes:
% ant compile
% ant dist
4. Run your new web service client from the command line:
% java -jar ../dist_client/MathServiceClient.jar 10 20
Username:<password>
Password:<username>
a+b=30
a-b=-10
a*b=200
a/b=0.500000
Where <username> is the username and <password> is the password you have supplied. When running your client, you are prompted on the command line for credentials.