|
Download
FAQ History |
|
API
Search Feedback |
Creating a Web Service with JAX-RPC
This section shows how to build and deploy a simple Web service called
MyHelloService. A later section, Creating Web Service Clients with JAX-RPC, provides examples of JAX-RPC clients that access this service. The source code required byMyHelloServiceis in<INSTALL>/jwstutorial12/examples/jaxrpc/helloservice/.These are the basic steps for creating the service:
The sections that follow cover these steps in greater detail.
Note: Before proceeding, you should try out the introductory examples in Chapter 3. Make sure that you've followed the instructions in Setting Up.
Coding the Service Endpoint Interface and Implementation Class
A service endpoint interface declares the methods that a remote client may invoke on the service. In this example, the interface declares a single method named
sayHello.A service endpoint interface must conform to a few rules:
- It extends the
java.rmi.Remoteinterface.- It must not have constant declarations, such as
public final static.- The methods must throw the
java.rmi.RemoteExceptionor one of its subclasses. (The methods may also throw service-specific exceptions.)- Method parameters and return types must be supported JAX-RPC types. See the section Types Supported By JAX-RPC.
In this example, the service endpoint interface is
HelloIF.java:package helloservice; import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloIF extends Remote { public String sayHello(String s) throws RemoteException; }In addition to the interface, you'll need the class that implements the interface. In this example, the implementation class is called
HelloImpl:package helloservice; public class HelloImpl implements HelloIF { public String message ="Hello"; public String sayHello(String s) { return message + s; } }Building the Service
To build
MyHelloService, in a terminal window go to the<INSTALL>/jwstutorial12/examples/jaxrpc/helloservice/directory and type the following:The preceding command executes these
anttasks:compile-service
This task compiles
HelloIF.javaandHelloImpl.java, writing the class files to thebuildsubdirectory.generate-sei-service
The
generate-sei-servicetask runs thewscompiletool, which defines the service by creating themodel.gzfile in thebuilddirectory. Themodel.gzfile contains the internal data structures that describe the service. Thegenerate-sei-servicetask runswscompileas follows:The
-defineflag instructs the tool to read the service endpoint interface and to create a WSDL file. The-dand-ndflags tell the tool to write output to thebuildsubdirectory. The tool reads the followingconfig-interface.xmlfile:<?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <service name="MyHelloService" targetNamespace="urn:Foo" typeNamespace="urn:Foo" packageName="helloservice"> <interface name="helloservice.HelloIF"/> </service> </configuration>The
config.xml-interfacefile tellswscompileto create a model file with the following information:
- The service name is
MyHelloService.- The WSDL namespace is
urn:Foo. (To understand this namespace, you need to be familiar with WSDL technology. See Further Information)- The classes for the
MyHelloServiceare in thehelloservicepackage.- The service endpoint interface is
helloservice.HelloIF.(If you are familiar with SOAP and WSDL, note that by default the service will be rpc/encoded. For doc/literal, see Advanced JAX-RPC Examples.)
package-service
The
package-servicetarget runs thejarcommand and bundles the files into a WAR file nameddist/hello-portable.war. This WAR file is not ready for deployment because it does not contain the tie classes. You'll learn how to create a deployable WAR file in the next section. Thehello-portable.warcontains the following files:WEB-INF/classes/hello/HelloIF.class WEB-INF/classes/hello/HelloImpl.class WEB-INF/jaxrpc-ri.xml WEB-INF/model.gz WEB-INF/web.xmlThe class files were created by the
compile-servicetarget discussed in a previous section. Theweb.xmlfile is the deployment descriptor for the Web application that implements the service. Unlike theweb.xmlfile, thejaxrpc-ri.xmlfile is not part of the specifications and is implementation-specific. Thejaxrpc-ri.xmlfile for this example follows:<?xml version="1.0" encoding="UTF-8"?> <webServices xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd" version="1.0" targetNamespaceBase="urn:Foo" typeNamespaceBase="urn:Foo" urlPatternBase="/ws"> <endpoint name="MyHello" displayName="HelloWorld Service" description="A simple web service" interface="helloservice.HelloIF" model="/WEB-INF/model.gz" implementation="hello.HelloImpl"/> <endpointMapping endpointName="MyHello" urlPattern="/hello"/> </webServices>Several of the
webServicesattributes, such astargetNamespaceBase, are used in the WSDL file, which is created by theprocess-wartask described in the next section. (WSDL files can be complex and are not discussed in this tutorial. See Types Supported By JAX-RPC). Note that theurlPatternvalue (/hello) is part of the service's URL, which is described in the section Verifying the Deployment).process-war
This
anttask runs thewsdeploytool as follows:The
wsdeploytool performs these tasks:
- Reads the
dist/hello-jaxrpc-portable.warfile as input- Gets information from the
jaxrpc-ri.xmlfile that's inside thehello-jaxrpc-portable.warfile- Generates the tie classes for the service
- Generates a WSDL file named
MyHelloService.wsdl- Packages the tie classes, the
MyHelloService.wsdlfile, and the contents ofhello-jaxrpc-portable.warfile into a deployable WAR file nameddist/hello-jaxrpc.warNote that the
wsdeploytool does not deploy the service; instead, it creates a WAR file that is ready for deployment. In the next section, you will deploy the service in thehello-jaxrpc.warfile that was created bywsdeploy.Deploying the Service
Type the following command:
Verifying the Deployment
To verify that the service has been successfully deployed, open a browser window and specify the service endpoint's URL:
The browser should display a page titled Web Services, which lists the port name
MyHellowith a status ofACTIVE. This page also lists the URL of the service's WSDL file.The
hello-jaxrpcportion of the URL is the context path of the servlet that implements theHelloWorldservice. This portion corresponds to the prefix of thehello-jaxrpc.warfile. The/hellostring of the URL is the alias of the servlet. This string matches the value of theurlPatternattribute of thejaxrpc-ri.xmlfile. Note that the forward slash in the/hellovalue ofurlPatternis required. For a full listing of thejaxrpc-ri.xmlfile, see the section, package-service.Undeploying the Service
At this point in the tutorial, do not undeploy the service. When you are finished with this example, you can undeploy the service by typing this command:
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.