Annotating an EJB bean to create a web service

You can annotate types, methods, fields, and parameters in your EJB bean to specify a web service.

Before you begin

Prerequisites:
  • Your workspace contains an EJB 3.x Session bean that has at least one public method.
  • This EJB bean is in a JAX-WS enabled EJB project.
  • The project's Target Runtime is WebSphere® Application Server v7.0 or higher.

About this task

The steps for creating a web service from an EJB bean using annotations is as follows:
  1. Annotate your EJB bean with the @WebService annotation, and any other annotations required for your implementation.
  2. Create JMS or HTTP router modules for the web service as described in Creating web service router modules. New in WebSphere Application Server v8, if you package your EJB application in a WAR module, you do not need to create router modules.
  3. Publish the application to a server as described in Creating a web service from an annotated EJB bean by publishing to a server.
Complete the following procedure to annotate your EJB bean:

Procedure

  1. In the Enterprise Explorer view, double-click your Java™ bean to open the file in the Java editor.
  2. On the Java class that implements your web service, specify the @WebService annotation, as well as a @Stateless, @Stateful or @Singleton annotation. Also specify the attributes, if any, that you want the annotation to have.
    • Most errors that display when adding annotations can be resolved using the suggested quickfixes. To display the quickfixes, click the error marker.
    • To add annotations or attributes to existing annotations, you can use the Annotations view rather than manually adding this information to the class. This view provides basic validation and guidance when working with annotations.
  3. Optional: Use these and other annotations to customize your web service further:
    • On the Java class that implements your web service, specify the @WebMethod annotation on each method that you want to customize for the service. You can use this annotation to exclude a method from your service. By default, all public methods are exposed in a service, including inherited methods that are under the Object class.
    • On the methods that are exposed in your web service, use the @WebParam and @WebResult annotations to customizes the mapping of your parameters and results to message parts and XML elements.
    • On an exception class, specify the @WebFault annotation to map your class to a WSDL fault.

Results

Once the annotations have been added to the bean, the Services view should list the web service under the JAX-WS heading. From this view you can test the web service by right-clicking it and selecting Test with Web Services Explorer or Test with Generic Service Client. You can also generate deployment descriptors and manage the policy sets associated with the service from this view.