|
Write a Web Service Server
Now that you need to write the server component for your Web service, see how to use an accessible approach that avoids complexity
by Kevin Jones
December 17, 2004
We've looked at writing JAX-RPC-based Web service clients (see "Write a Web Service Client" in this issue), and along the way we touched on various aspects including WSDL consumption, stubs, and skeletons. Now let's look at writing Web service servers. Like many other aspects of software development, there is more than one way to skin this particular cat. Here we're going to take the approach of starting with Java code and generating all the necessary artifacts from that code.
I want to emphasize up front that while this choice is reasonably straightforward, it is not necessarily the best approach or the approach I would recommend. (The best approach is to first write your Web Services Description Language [WSDL] document, and then generate all the necessary code from that. We'll look at this approach later.) However, we'll use the start-with-Java-code approach for two reasons: it is by far the most accessible way of explaining Web service servers because most of you (I hope) are familiar with Java. Also, writing Web service servers using JAX-RPC can get very involved, and I initially want to keep the complexity of WSDL out of the equation until we understand the larger picture.
In the article about writing a Web service client we talked about downloading the Java Web Services Developer Pack (JWSDP) 1.4 from the Sun Microsystems site (see Resources). When you install it you'll be asked to install a Web server and to then install JWSDP on top of that server. The options are to install over Sun Java System Application Server 8 Platform Edition, Sun Java Web Server 6.1, or Tomcat 5 (see Resources).
If like me you already have a Web server installed, then downloading another server is not necessary. JWSDP comes with shell scripts for Windows and Unix that allow you to run a post install step to install JWSDP over an already installed Web server. This is fine if your Web server is one of the supported options, and you are happy for the JWSDP to mess around with your server. I fell into the second category and decided to see if I could install JWSDP over my already existing Web server without running the installation script.
Taking Steps
For my purposes the Web server is Tomcat (currently at 5.0.27, but this approach should work with any Web server). If you are running Tomcat on JDK 1.5 (or whatever Sun finally decided to call it), then you'll have no problems. A deployed JAX-RPC application will simply work (provided the JAX-RPC JARs are available, which we will talk about shortly). However, if you are using JDK 1.4.x, then there is an issue. It turns out that JWSDP uses a Sun implementation of the Xerces XML parser, while Tomcat ships with the "official" implementation. To get Tomcat to use the correct parser, it is a matter of copying the JAR files from %JWSDP_HOME%/jaxp/lib/endorsed to %TOMCAT_HOME%/common/endorsed. That's it, and a similar solution should work for other Web servers.
Building a JAX-RPC standard implementation (SI) service requires a number of steps: write a Java interface and class that defines the Web service methods; write build configuration files to configure the server and generate the WSDL; write a deployment configuration file and create a WAR file that will contain the Web application that is the Web service—this second configuration file is included in this WAR file; process the WAR file to turn it into a JAX-RPC SI Web service; and deploy the Web application. Let's look at these steps one at a time. You can download the source code for this demonstration.
Back to top
|