Axis-orizing objects for SOAP
Go from Java objects to a SOAP Web service with Apache Axis
By Mitch Gitman, JavaWorld.com, 04/11/03
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
It all starts innocently enough. You have an object-oriented Java interface you want to turn into a Web service. You come
across Axis, the Apache framework for publishing and consuming Web services using the XML-based SOAP (Simple Object Access
Protocol) specification. Soon enough you find yourself needing to become more of a SOAP expert than you had hoped, opening
the Axis source more than you have patience to, and, well, spending more time than you have. At least that's what happened
to me. This article explains how to transform your Java objects for the Web-service world with Axis—minus the hassle.
First steps
First, make sure you have installed:
- A servlet engine or J2EE (Java 2 Platform, Enterprise Edition) application server. The Axis server component is typically deployed as a Web application.
- A programmable build tool like Apache Ant. Creating a Web service takes you through a string of deployment operations besides just compiling, so it's critical that
you automate this process as fully as possible.
- Axis 1.1 Release Candidate 2. Download the archive and expand it. Follow the installation instructions at least up to "Validate Axis with happyaxis."
- Optionally, an XML parser. For example, a DOM (Document Object Model) parser like JDOM or dom4j. At times, you will need to edit automatically generated
XML files. If you're willing to write some code using a DOM (or SAX (Simple API for XML)) parser, you can automate this work.
- Optionally, JUnit. Later, I discuss testing an Axis Web service via a JUnit client.
SOAP and objects
While this article doesn't purport to be a general primer on SOAP best practices, some words on SOAP hygiene are in order.
SOAP originally stood for Simple Object Access Protocol. Yet, SOAP never mandated the use of objects per se, so the acronym
eventually was cleansed of its origin. Don't let that fact throw you off the scent. Axis still converts your Java objects
into XML for transmission over some transport, typically HTTP. In SOAP-formatted XML, these Java objects appear as data structures,
which can be built through what are effectively class inheritance and object composition, to borrow a couple object-oriented
terms.
Of course, when you get into sending complex, self-defined types over SOAP, you can always ask yourself the question, "Do
I really need to send these structures?" You will find your answer with a Java interface.
Java interface as service contract
You must define whatever methods you want to provide through your Web service in a single interface or class. Interface is
preferred over class, not only so that the service contract does not depend on its implementation, but also so that Axis and
its tools don't inadvertently publish extra methods or types.
For a Web service-worthy interface, minimize the interaction between client and server, in terms of both frequency and bandwidth.
Even if the client and service are on the same machine, you pay the minor performance price of having to marshal and unmarshal
each request and response into relatively bulky XML text, as opposed to binaries.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- From the Axis homepage, links of particular interest are Installation, User's Guide, and Mailing Lists
http://ws.apache.org/axis/
- Axis already comes with some predefined Ant tasks. Even without these tasks, an automated build tool like Ant is a must
http://ant.apache.org/
- JUnit, for testing an Axis Web service—or really any Web service—using an Axis client
http://www.junit.org/index.htm
- Though hardly the only XML parser that will get the job done, dom4j is the author's favorite
http://www.dom4j.org/
- Official homepage for the JAX-RPC specification
http://java.sun.com/xml/jaxrpc/
- Read "AxisThe Next Generation of Apache SOAP," Tarak Modi (JavaWorld, January 2002) for an introduction to Axis
http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-axis.html
- For SOAP basics, read "Clean Up Your Wire Protocol with SOAP," Tarak Modi (JavaWorld)
-
- To get started with Ant, read "Automate Your Build Process Using Java and Ant," Michael Cymerman (JavaWorld, October 2000)
http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-ant.html
- For more articles on Web services, see the following JavaWorld resources
-
- Browse the Java and XML section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-xml-index.shtml
- Talk more about developing with Axis in our Enterprise Java discussion
http://forums.devworld.com/webx?50@@.ee6b80a
- Sign up for JavaWorld's free weekly Enterprise Java email newsletter
http://www.javaworld.com/subscribe
- You'll find a wealth of IT-related articles from our sister publications at IDG.net