Part 4: Building an Annotation Driven EJB Web Service
You can use any Java class as a Web Service, even an EJB Session Bean.
show more or less

A Session Bean usually implements some type of business task or activity. It is generally instantiated or created for a specific client request and and usually exists only for the duration of a single session. They can be used for anything but are commonly used for one-off calculations, information lookup, etc, especially when data is not going to be persisted.

Step 1: Creating the EJB Web Service
  1. Create a new empty project. Right-click the Annotation project node and select New. In the New Gallery, click the All Features tab and then select General > Projects in the Categories list and Custom Project in the Items list. Click OK

    new gallery

  2. Name the new project  EJB-Anno, leaving other values at their default. Click Finish.

    create generic project EJB-Anno

  3. Right-click the EJB-Anno project and select New.

    application navigator select new

  4. In the New Gallery, expand the Business Tier node and select EJB in the Categories list . In the Items column, select Session Bean and click OK.

    new gallery create session bean

  5. In the new Session Bean Wizard, click Next up to Step 4. Accepting the defaults for the EJB Version (Enterprise JavaBeans 3.0)Name and Class Definition pages.

  6. In the EJB Home and Component Interfaces page, deselect both check boxes so that no interfaces are implemented. Click Next, then Finish to create the bean.

    create session bean interface page

  7. The file opens in the editor. Add the same sayHello() method as in previous scenarios:

    The class should now look like:

    java code editor with code
  8. Click Save All save all to save your work.

    At this point you just have a class with simple business logic to return the word Hello, followed by the value entered for a parameter. Next you add the web service annotations as you did before.

  9. Above the class definition add the @WebService annotation. JDeveloper will automatically add the import for javax.jws.WebService. Click the @WebService line of code and then click the lightbulb in the left margin. Sselect Configure project for web services to configure the rest of the project for web services.

    java code editor with annotations

  10. Above the sayHello method, add the @WebMethod annotation, just as you did with the POJO. If requested, press [Alt]+[Enter] to add the import statement (although this statement may be added automatically.)

    java code editor with complete annotation

  11. Click Save All save all to save your work.

Step 2: Testing the Web Service

In this section you compile, deploy and test the web service. Just as before, you use the HTTP Analyzer for testing the web service. When you test web services using the analyzer, the service is compiled and deployed to the integrated server. The analyzer is then invoked, enabling you to send and receive values from the web service.

  1. In the Application Navigator, right-click the node and in the context menu, select Test Web Service application navigator select Test Web Service

    Like earlier, the top portion of the HTTP Analyzer editor displays the URL for the web service, WSDL URL, and exposed Operations.

    HTTP Analyzer

  2. In the Request area, enter <your name> in the arg0 field and click Send Request.

    Request area with name

    The analyzer then sends the request to the service, and after a bit the return parameter is displayed.

    Response area

  3. Close all the tabs and collapse the EJB-Anno project.

Bookmark Print Expand all | Hide all
Back to top
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.