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 ...

Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.