O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing Web Services

As you might expect, implementing a SOAP web service is moderately more difficult than just using one as a client. Thankfully, JAX-RPC and the Java web service engines make the writing of the web service implementations themselves very simple (even simpler than writing a client, in some cases). The added complexity of implementing web services comes in the deployment step, which we’ll discuss in the next major section.

Simple Java Web Services

If you look at the static proxy approach for implementing SOAP clients, you may notice a lot of similarities with the client stubs used in RMI for talking to remote RMI objects. That’s not a coincidence. JAX-RPC uses the RMI programming model to provide a simple way to write web services in Java. The published interface for the web service is defined as a Remote interface. If we were implementing our simple echo web service in Java, we could define the interface for the service as shown in Example 12-2.

Example 12-2. A JAX-RPC service interface

public interface IEcho extends java.rmi.Remote {
    public String echo(String message) throws java.rmi.RemoteException;
}

The implementation for the service is equally simple to define. We just write a concrete implementation of this Remote interface, as shown in Example 12-3.

Example 12-3. A JAX-RPC service implementation

public class Echo implements IEcho {
    public Echo() {
       super();
    }

    public String echo(String message) throws RemoteException {
       return message;
    }
}

That’s it. The IEcho interface ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required