Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

318 HOUR 22:Creating Web Services with JAX-WS


The final part of the address, /service, is the path. Every web service
must have a unique path. If you run any other web services on your com-
puter, they can’t have the same path as SquareRootServer.
To deploy the web service, create an empty Java file called
SquareRootServerPublisherin the com.java24hours.wspackage. Enter
the text of Listing 22.3 in this file.

LISTING 22.3 The Full Text of SquareRootServerPublisher.java
1: packagecom.java24hours.ws;
2:
3: importjavax.xml.ws.*;
4:
5: public classSquareRootServerPublisher {
6: public static voidmain(String[] arguments) {
7: SquareRootServerImpl srsi = new SquareRootServerImpl();
8: Endpoint.publish(
9: “http://127.0.0.1:5335/service”,
10: srsi
11: );
12: }
13: }

When you run the application, it waits for connections on port 5335 of
your computer. You can call the methods of the web service from any pro-
gram that supports SOAP- or REST-based web services, whether the pro-
gram is written in Java or another language. As long as your web service
is on the Internet, any other Internet-connected software can call the
methods.

Using Web Service Definition
Language Files
Before trying out this web service, you can test the availability of the
SquareRootServerPublisherapplication with any web browser.
Open a browser and load the address http://127.0.0.1:5335/
service?wsdl. The browser displays the XMLfile shown in Listing 22.4.
This file is being served by the application that you just created.
This file is a service contract that’s written in Web Service Description
Language (WSDL), an XMLdialect for spelling out exactly how a web
service functions so that servers and clients can make full use of it.
Free download pdf