ptg7068951
Creating a Web Service Client 321
Because web service technology like the JAX-WS library supports stan-
dards such as SOAP, REST, HTTP, and XML, you don’t have to use a Java
program to connect to the square root web service. Perl, Python, Ruby and
other languages all have libraries that support web services.
The JAX-WS library offers the Serviceclass in the javax.xml.wspackage,
a factory that creates objects that can call a web service.
The class method Service.create(URL, QName)creates the factory. The
arguments are a URLobject from java.netand a QNamefrom
javax.xml.namespace.
The URLmust be the address of the web service’s WSDLcontract:
URL url = new URL(“http://127.0.0.1:5335/service?wsdl”);
The QNameis a qualified name, an XMLidentifier that’s associated with the
provider of the web service. Aqualified name consists of a namespace URI
and a local identifier.
Namespace URIs are similar to URLs but do not necessarily work as a web
address. Because the package name of the square root web service is
com.java24hours.ws, which by convention in Java associates it with the
Internet host name ws.java24hours.com, the namespace URI for this web
service is http://ws.java24hours.com.
The local identifier for the web serviceis the name of the Service
Implementation Bean with the word “Service” appended. Here’s the state-
ment that creates the qualified name:
QName qname = new QName(
“http://ws.java24hours.com/”,
“SquareRootServerImplService”
);
With the URLand qualified name, you can create the web service client
factory:
Service service = Service.create(url, qname);
The factory has a getPort(Class)method that creates an object of the
specified class. To identify a Java class for use as a method argument, use a
class variable of the class named class. Confusing? It makes more sense
when you see it in a Java statement:
SquareRootServer srs = service.getPort(SquareRootServer.class);
CAUTION
As stated,a URI does not have
to be a working web address.
Althoughhttp://ws.
java24hours.comlooks like
one,it’s used here simply as a
unique identifier. I own the
domain name java24hours.com
and control how its subdomains
are used. So when I designate
http://ws.java24hours.com
as a URI,I can be reasonably
assured that no other web serv-
ice provider would take that
identification.