Adding web services to Java applications should not require programming. There should be a simple ON/OFF switch. You should be able to select some business logic, flip the ON switch, and publish it as a web service.
Unfortunately, Java isn’t designed that way. Java requires you to write code, annotate classes, recompile, and redeploy. And it isn’t simple coding either — particularly when you run into problems with Java/XML serialization.
In my book SOA Using Java Web Services, I detail my frustrations with Java’s web services support. Page 3 quotes Dave Podnar’s 2006 blog entry:
Dave Podnar's Five Stages of Dealing with Web Services
- Denial: It's Simple Object Access Protocol, right?
- Over-Involvement: OK, I'll read the SOAP, WSDL, WS-I BP, JAX-WS, SAAJ, JAXB, … specs. Next, I'll check the wiki and finally follow an example showing service and client sides.
- Anger: I can't believe those #$%&*@s made it so difficult!
- Guilt: Everyone is using Web Services. It must be me; I must be missing something.
- Acceptance: It is what it is. Web Services aren't simple or easy.
Since writing that book, I’ve spent a great deal of time thinking about how to reinvent Java Web Services as a simple ON/OFF switch. After three years working with some smart people at Proxisoft, we’ve made a lot of progress.
This article highlights the problems with Java Web Services and describes how we are solving them. The algorithms and framework described here are incorporated in our Netrifex product.
Current Difficulties with Java Web Services
A legion of issues conspires to make Java Web Services difficult, particularly for enterprise application development. This article highlights three: programming complexity, source code binding, and framework ignorance.
JAX-WS, JAX-RS, and JAXB are the Java standards for implementing web services (also known as the JAX-* standards). JAX-WS handles SOAP, JAX-RS handles REST, and JAXB provides Java/XML serialization for both SOAP and REST.
For the most part, using these standards involves adding annotations (for example, @WebService, @GET, @XmlRootElement) to your source code. Most Java Web Services tutorials mask the complexity of this process by focusing on simple “hello world” examples.
However, when you try using the standards with real applications, the complexity grows exponentially. In a recent experiment, I used JAX-* to web-serviceenable a simple stock trading demo application that we use for testing at Proxisoft. In order to expose a single class as a service, I ended up having to modify more than 30 other classes with various JAXB annotations. This happens because, in order for Java to serialize a single class, it must know how to serialize all of its dependent classes as well.
Even after introducing the required annotations on the 30 classes, I found that the resulting web service threw exceptions and failed at runtime. Why? Many of the dependent classes did not have no-arg constructors — a JAXB requirement that causes runtime exceptions during deserialization. After solving that problem, other issues surfaced. Method parameters and return types that were interfaces or abstract classes caused similar JAXB failures and had to be modified.
Even after making all these changes in the code to satisfy JAX-*, I still encountered runtime exceptions. The reason was that the application’s class dependency graph included a cycle. To solve this problem, I had to implement the CycleRecoverable interface on one of the dependent classes. Implementing this interface required writing code to handle the runtime serialization errors resulting from cycles.
Source Code Binding
As discussed in the previous section, programming Java Web Services for real-world applications is complex and time-consuming. But for the sake of argument, suppose you master JAX-*. Suppose you slog through all the complexities of mapping your application to web services with these standards. What has been accomplished?
Well, you now have web services baked into your application’s source code. What happens when your application is running in production and you need to add, modify, or remove some web services? You need to modify the source code, unit test, application test, recompile, repackage, and redeploy.
Embedding web services in source code with annotations is not very agile.
In addition to being complex and inflexible, programming JAX-* is too low-level. These programming standards are completely ignorant of application frameworks like Struts or Spring. As a result, when you code with JAX-*, you cannot plug in to the powerful machinery provided by such frameworks.
Consider a Struts application being used for e-commerce. Say you want to web-serviceenable it to support B2B integration. You basically have to re-create the entire View-Controller part of the application to support web services interaction.
It shouldn’t be this way. After all, the business logic and APIs that need to be exposed as web services are already encoded in the existing services classes that are invoked by Action classes, and mapped to a web interface by the ActionForms and ActionMapping. You should simply be able to flip an “ON” switch to expose selected parts of this existing infrastructure as web services.
The reason that JAX-* doesn’t work well with Struts is that session management and application context are managed by the Struts infrastructure independent from the business logic managed by the service classes. For example, you could use JAX-* to web-serviceenable a ShoppingCart class, but because the resulting service will be stateless, there is no natural way of associating a user’s web service invocation with the instance of ShoppingCart belonging to his session. What’s needed is a framework that understands how Struts manages sessions and can expose the ShoppingCart in a stateful manner.