O'Reilly logo

Java SOA Cookbook by Eben Hewitt

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

Creating a Basic Web Service

Problem

You want to create a basic but real-world web service in Java EE 5.

Solution

Create an EJB or servlet and annotate it with @WebService. Then package it as you normally would and deploy it; JAX-WS will take care of creating the WSDL and the necessary mappings at deploy time.

Here is the simplest possible way to do it with an EJB:

package com.soacookbook.catalog.ejb;

import com.soacookbook.ns.catalog.*;
import javax.ejb.*;
import javax.jws.*;

/**
 * Basic Web Service does shopping cart operations.
 */
@WebService
@Stateless
@Local
public class CartEJB {
    public Double getVersion() {
        return 5.0;
    }
}

This is enough to create a web service that accepts the default mappings. It is important to examine for a moment what those mappings are. In later recipes, you’ll customize the mappings for the generated WSDL so that the names of your elements are more user friendly.

Note that the bean does not implement a business interface. This may be something that you’d want to do in the real world, as it makes your bean available to other enterprise processes such as web-based context listeners, but as of EJB 3, it is no longer strictly necessary.

See Creating and Deploying the Simplest Web Service for how to create and invoke the simplest possible web service in Java.

Discussion

Let’s take a look at each of the important sections of the WSDL to see how it maps. This might not seem important at this stage because you can just use the wsdimport tool to generate the artifacts ...

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