5. Access web service using stubs
The willingness of stubs in software is "stubs" and "piles", which is a proxy created in the system to hide remote calls. The use of stubs encapsulates the complex details of remote calls. Other classes can use the remote functions through stubs just like local classes.
Here, we still use the established helloworld. JWS to describe how to use stubs.
Axis provides the WSDL tool to automatically generate stubs. Wsdl2jav uses the Web Service Description Language (WSDL) file of web service to automatically generate Java stub classes.
Run Java org. Apache. axis. WSDL. wsdl2java-P client http: // localhost: 8080/axis/helloworld. JWS? WSDL
Make sure that all jar packages in the axis/lib directory are included in the classpath. After successful execution, a client directory is generated under the current directory, which contains four Java class files:
Helloworld. Java
Helloworldservice. Java
Helloworldservicelocator. Java
Helloworldsoapbindingstub. Java
This is the stubs that can access helloworld locally. You can access helloworld through this stubs:
Import client .*;
Public class testhelloclient ...{
Public static void main (string [] ARGs )...{
Try ...{
Helloworldservice service = new client. helloworldservicelocator ();
Helloworld client = service. gethelloworld ();
String retvalue = client. sayhello ("Brookes ");
System. Out. println (retvalue );
} Catch (exception e )...{
System. Err. println ("execution failed. Exception:" + E );
}
}
}
Execute this program and you will get the results returned by the Web Service:
Hello Brookes, welcome to my web service.
Axis also provides a java2wsdl tool to automatically generate a WSDL file from a Java class. You can also use this command to generate the WSDL file, for example:
Java org. Apache. axis. WSDL. java2wsdl-ohelloworld. WSDL-lhttp: // localhost: 8080/axis/service/urn: helloworld-nhelloworld Brookes. helloworld
Then, use the generated helloworld. WSDL file to generate stubs.
Java org. Apache. axis. WSDL. wsdl2java helloworld. WSDL-P Client
There is a lot of information about how to use the axis command line tool on the Internet, such as Baidu/Google.