ptg7068951
322 HOUR 22:Creating Web Services with JAX-WS
The call to getPort()with SquareRootServer.classas the argument causes
the factory to create a SquareRootServerobject. This object is stored in the
srsvariable.
Call the methods of the SquareRootServerobject as you would any other
object in Java:
System.out.println(srs.getTime());
System.out.println(srs.getSquareRoot(625D));
The JAX-WS library packages these method calls as SOAPmessages, sends
them over the Internet to the web service, and transmits the calls.
When the service responds to the calls, it packages the responses as SOAP
messages, sends them back over the Internet, and they’re converted back to
Java data types.
Put all these together by creating an empty Java file named
SquareRootClientand entering the text from Listing 22.5 in the file.
LISTING 22.5 The Full Text of SquareRootClient.java
1: packagecom.java24hours.ws;
2:
3: importjava.net.*;
4: importjavax.xml.namespace.*;
5: importjavax.xml.ws.*;
6:
7: classSquareRootClient {
8: public static voidmain(String[] arguments) throwsException {
9: URL url = new URL(“http://127.0.0.1:5335/service?wsdl”);
10: QName qname = new QName(
11: “http://ws.java24hours.com/”,
12: “SquareRootServerImplService”
13: );
14: Service service = Service.create(url, qname);
15: SquareRootServer srs = service.getPort(SquareRootServer.class);
16:
17: System.out.println(srs.getTime());
18: System.out.println(srs.getSquareRoot(625D));
19: }
20: }
When you run the client application, you see the output shown in
Figure 22.1 if the SquareRootPublisherapplication is running.