A Web Service Example: HelloServiceBean
This example demonstrates a simple web service that generates a response based on information received from the client.
HelloServiceBeanis a stateless session bean that implements a single method,sayHello. This method matches thesayHellomethod invoked by the clients described in Static Stub Client. Later in this section, you'll test theHelloServiceBeanby running one of these JAX-RPC clients.Web Service Endpoint Interface
HelloServiceis the bean's web service endpoint interface. It provides the client's view of the web service, hiding the stateless session bean from the client. A web service endpoint interface must conform to the rules of a JAX-RPC service definition interface. For a summary of these rules, see Coding the Service Endpoint Interface and Implementation Class. Here is the source code for theHelloServiceinterface:package helloservice; import java.rmi.RemoteException; import java.rmi.Remote; public interface HelloService extends Remote { public String sayHello(String name) throws RemoteException; }Stateless Session Bean Implementation Class
The
HelloServiceBeanclass implements thesayHellomethod defined by theHelloServiceinterface. The interface decouples the implementation class from the type of client access. For example, if you added remote and home interfaces toHelloServiceBean, the methods of theHelloServiceBeanclass could also be accessed by remote clients. No changes to theHelloServiceBeanclass would be necessary. The source code for theHelloServiceBeanclass follows:package helloservice; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloServiceBean implements SessionBean { public String sayHello(String name) { return "Hello "+ name + " from HelloServiceBean"; } public HelloServiceBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} }Building HelloServiceBean
In a terminal window, go to the
<INSTALL>/j2eetutorial14/examples/ejb/helloservice/directory. To buildHelloServiceBean, type the following command:This command performs the following tasks:
The
wscompiletool writes theMyHelloService.wsdlfile to the<INSTALL>/j2eetutorial14/examples/ejb/helloservice/build/subdirectory. For more information about thewscompiletool, see Chapter 8.Use
deploytoolto package and deploy this example.Creating the Application
In this section, you'll create a J2EE application named
HelloService, storing it in the fileHelloService.ear.
- In
deploytool, select FileNew
Application.
- Click Browse.
- In the file chooser, navigate to
<INSTALL>/j2eetutorial14/examples/ejb/helloservice/.- In the File Name field, enter
HelloServiceApp.- Click New Application.
- Click OK.
- Verify that the
HelloServiceApp.earfile resides in<INSTALL>/j2eetutorial14/examples/ejb/helloservice/.Packaging the Enterprise Bean
Start the Edit Enterprise Bean wizard by selecting File
New
Enterprise Bean. The wizard displays the following dialog boxes.
- Introduction dialog box
- EJB JAR dialog box
- Select the button labeled Create New JAR Module in Application.
- In the combo box below this button, select
HelloService.- In the JAR Display Name field, enter
HelloServiceJAR.- Click Edit Contents.
- In the tree under Available Files, locate the
<INSTALL>/j2eetutorial14/examples/ejb/helloservice/build/directory.- In the Available Files tree select the
helloservicedirectory andmapping.xmlandMyHelloService.wsdl.- Click Add.
- Click OK.
- Click Next.
- General dialog box
- In the Configuration Options dialog box, click Next. The wizard will automatically select the Yes button for Expose Bean as Web Service Endpoint.
- In the Choose Service dialog box:
- In the Web Service Endpoint dialog box:
- Click Finish.
- Select File
Save.
Deploying the Enterprise Application
Now that the J2EE application contains the enterprise bean, it is ready for deployment.
Building the Web Service Client
In the next section, to test the web service implemented by
HelloServiceBean, you will run the JAX-RPC client described in Chapter 8.To verify that
HelloServiceBeanhas been deployed, click on the target Application Server in the Servers tree indeploytool. In the Deployed Objects tree you should seeHelloServiceApp.To build the static stub client, perform these steps:
- In a terminal go to the
<INSTALL>/j2eetutorial14/examples/jaxrpc/helloservice/directory and type
asant build- In a terminal go to the
<INSTALL>/j2eetutorial14/examples/jaxrpc/staticstub/directory.- Open
config-wsdl.xmlin a text editor and change the line that reads
<wsdl location="http://localhost:8080/hello-jaxrpc/hello?WSDL"to
<wsdl location="http://localhost:8080/hello-ejb/hello?WSDL"- Type
asant build- Edit the
build.propertiesfile and change theendpoint.addressproperty to
http://localhost:8080/hello-ejb/helloFor details about creating the JAX-RPC service and client, see these sections: Creating a Simple Web Service and Client with JAX-RPC and Static Stub Client.
Running the Web Service Client
To run the client, go to the
<INSTALL>/j2eetutorial14/examples/jaxrpc/staticstub/directory and enterThe client should display the following line: