Java & J2EE Page 6 - Using RPC-Style Web Services with J2EE |
The initial service is servlet-based and will just pass strings back and forth to exchange greetings. After you are comfortable with this simple service, we will move on to look at how to provide a Web Service Files and Tools In common with other J2EE components, a J2EE-based Web Service implementation consists of multiple parts:
A Web Service client has similar code and configuration files but does not carry an interface definition or service implementation information. All the configuration files are encoded in XML. Some of them are reasonably simple and can easily be created by hand. Other files, Defining the Interface and Implementation As with RMI, a remote interface definition is central to the use of JAX-RPC. This interface is either written by a developer and then used to generate a WSDL file, or the Java interface is generated from a pre-provided WSDL file. In the examples we look at today, the WSDL file is generated from the Java interface definition. The Java interface definition must extend java.rmi.Remote and all methods must be declared as throwing java.rmi.RemoteException. The simple Web Service is based around a simple Java Remote interface, Greeting, containing a single method called sayHelloTo(). The sayHelloTo() method takes a single String parameter—the name of the person to greet—and returns the resultant greeting. The Greeting interface is shown in Listing 20.1. Listing 20.1 Interface for the Greeting Service (Greeting.java) package wsexamples;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Greeting extends Remote
{
public String sayHelloTo(String name) throws RemoteException;
}
The sayHelloTo() method is the Web Service equivalent of the "Hello World!" program used as a first step in learning programming languages such as C and Java. In Web Service terms, the method is simple because it takes a single parameter and returns a single value. These values are both strings, and strings are easily marshaled between the on-the-wire XML representation used by SOAP and the internal java.lang.String type. The Greeting interface is implemented by the GreetingImplementation Listing 20.2 Implementation of the Greeting Service (GreetingImplementation.java) package wsexamples;
public class GreetingImplementation implements Greeting
{
public String sayHelloTo(String name)
{
return "Hi there " + name;
}
}
As you can see, there is nothing special about the Web Service Compiler Configuration File When you created EJBs earlier, you had to include various configuration files to tell the container about the EJB and its requirements. The same principle applies for a JAX-RPC Web Service. Somehow, a variety of artifacts must be created that describe the service for potential clients and help to integrate the service with its container. In the J2EE Reference Implementation, the main tool for this is wscompile. The wscompile tool can generate WSDL, server-side scaffolding, and client-side scaffolding. For the time being, you will concentrate on the WSDL and server-side scaffolding. wscompile requires a variety of information about the artifacts you need to create. You define this information in an XML-encoded configuration file. The root element is configuration and its sole attribute indicates that the file conforms to the RI's JAX-RPC schema: <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> Within this root element you will typically define one of two child elements:
The element you require will depend on whether you are starting from a Java interface definition or starting from WSDL. In this case, you will start from the code and generate the WSDL. Although this is convenient as a learning exercise, it is not the best practice to follow when creating industrial-strength Web Services. Some of the reasons for this are discussed in the section "Starting from WSDL" later today. In this case, because you are generating a WSDL file from a Remote interface, you need a service element within the root. The attributes of this element define the following:
The resultant element will look something like this: <service name="GreetingService" targetNamespace="urn:J2EE21Examples" typeNamespace="urn:J2EE21ExamplesTypes" packageName="wsexamples" > Each service element can contain one or more interface elements. The interface element describes a Web Service endpoint that is exposed as part of the service. The name attribute defines the name of the Java interface that implements the methods to be exposed. This should be set to the fully qualified name of the interface you created earlier (you can also specify the name of the implementation, or servant, class using the servantName attribute although this does not affect the generation of the WSDL): <interface name="wsexamples.Greeting" servantName= Listing 20.3 shows a full wscompile configuration file that will generate the WSDL for the simple Greeting interface shown earlier. Listing 20.3 wscompile Web Service Configuration File (config-service.xml) <?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <service name="GreetingService" targetNamespace="urn:J2EE21Examples" typeNamespace="urn:J2EE21ExamplesTypes" packageName="wsexamples"> <interface name="wsexamples.Greeting" servantName= You define which artifacts you want wscompile to generate using command-line options. Once you have created the configuration file shown in Listing 20.3, you can use wscompile to generate the WSDL description and mapping file for the Greeting service. Under Microsoft Windows: wscompile -define -classpath %CLASSPATH%;classes -nd ws\generated –mapping generated\mapping.xml ws\config-service.xml Under Unix: wscompile -define -classpath $CLASSPATH:classes -nd ws/generated –mapping generated/mapping.xml ws/config-service.xml The line shown assumes that the compiled classes for Greeting and GreetingImplementation reside in the classes subdirectory and that the configuration file is in the ws subdirectory. The –define flag indicates that you want just the service definition at the moment (no server or client artifacts), the –nd flag indicates that the resultant WSDL file should be placed in the ws/generated subdirectory, and the –mapping flag indicates where the generated mapping file is to be saved. The code for all of today's examples is on the Web site that accompanies this book. You will find them in the examples directory for Day 20. With the code supplied on the accompanying Web site, you can use the following Ant command to build the supplied Greeting service, which includes the creation of the WSDL and the mapping file: asant build
blog comments powered by Disqus |
|
|
|
|
|
|
|