JBoss.orgCommunity Documentation

Chapter 2. Sample Web Service wizards

2.1. Sample RESTful Web Service

JBoss Tools includes wizards for the creation of sample web services. These include:

These wizards are used within a Dynamic Web project. A dynamic web project can be created by following the steps in Creating a dynamic web project.

Procedure 2.1. Creating a dynamic web project

  1. Access the New Project Dialog

    Select FileNewProject

    Result:

    The New Project screen displays.

  2. Define the Project Type

    1. Click the Dynamic Web Project label by expanding the Web folder.

    2. Click the Next button to proceed.

    Result:

    The New Dynamic Web Project screen displays.

    Figure 2.1. Dynamic Web Project Attributes


  3. Define the Project Attributes

    Define the Dynamic Web Project attributes according to the options displayed in Table 2.1, “New Dynamic Web Project”

    Table 2.1. New Dynamic Web Project

    FieldMandatoryInstructionDescription
    Project nameyesEnter the project name.The project name can be any name defined by the user.
    Project locationyesClick the Use default location checkbox to define the project location as the Eclipse workspace or define a custom path in the Location field.The default location corresponds to the Eclipse workspace.
    Target runtimenoSelect a pre-configured runtime from the available options or configure a new runtime environment.

    The target runtime defines the server to which the application will be deployed.

    Dynamic web module versionyesSelect the required web module version.

    This option adds support for the Java Servlet API with module versions corresponding to J2EE levels as listed in Table 2.2, “New Dynamic Project - Dynamic web module version”.

    ConfigurationyesSelect the project configuration from the available options.

    The project can be based on either a custom or a set of pre-defined configurations as described in Table 2.3, “New Dynamic Project - Configuration”.

    EAR membershipnoAdd the project to an existing EAR project.

    The project can be added to an existing EAR project by selecting the checkbox. Once checked, a new EAR project can be defined by clicking the New Project button.

    Working setsnoAdd the project to an existing working set.

    A working set provides the ability to group projects or project attributes in a customized way to improve access. A new working set can be defined once the Select button has been clicked.




  4. Access the Java sub-dialog

    Click Next to proceed.

    Result:

    The New Dynamic Web Project - Java dialog displays.

  5. Define the source and output folders

    Define the Dynamic Web Project source and output folders by adding or editing folders as required.

  6. Access the Web Module sub-dialog

    Click Next to proceed.

    Result:

    The New Dynamic Web Project - Web Module dialog displays.

    Figure 2.2. New Dynamic Web Project - Web Module


  7. Enter the web module settings

    Define the settings as listed in Table 2.4, “New Dynamic Web Project - Web Module” including the root folder for path names in the web project context and the name of the web content directory.


  8. Open the Java EE perspective.

A sample RESTful web service can be generated by following the steps outlined in Generate a sample RESTful web service.

Procedure 2.2. Generate a sample RESTful web service

Target runtime must have RESTEasy installed

The sample RESTful web service will not work unless it is deployed to a server with RESTEasy installed, such as JBoss SOA-P.

  1. Access the New - Select a wizard dialog

    1. Right click on the project name in the Project Explorer view.

    2. Select NewOther.

    3. Click the Create a Sample RESTful Web Service label by expanding the Web Services folder.

    Result:

    The New - Select a wizard dialog displays with the selected wizard type highlighted.

  2. Access the Generate a Sample RESTful Web Service dialog

    Click the Next button to proceed.

    Result:

    The Generate a Sample RESTful Web Service - Project and Web Service Name dialog displays.

    Figure 2.3. Generate a Sample RESTful Web Service - Project and Web Service Name


    Due to the nature in which JBoss Application Server 7 and JBoss Enterprise Application Server 5 handle JAX-RS support, the wizard can now be completed without the need for RESTEasy JARs in the project classpath or associated project runtime. The JARs are necessary for JBoss SOA-P servers.

  3. Define the service attributes

    Define the project, web service, package and class names according to the options displayed in Table 2.5, “Project and Web Service Name”

    Table 2.5. Project and Web Service Name

    Dialog groupFieldMandatoryInstructionDescription
    Dynamic Web Project yesEnter the project name.The project name will default to the highlighted project in the Project Explorer. A different project can be selected from the list or entered directly in the editable drop-down list.
    Web ServiceNameyesEnter the name for the web service.The web service name will be the url for the service as mapped in the deployment descriptor (web.xml).
     Update web.xmlnoCheck this box to add the service to the deployment descriptor.This option is checked by default and may be unchecked when deploying to JBoss AS 6.0 or RESTEasy 2.0 servers. Service information is not required in the deployment descriptor for these servers.
    Add RESTEasy Jars from root runtime directorynoCheck this box to add RESTEasy JARs to the project.This option allows you to add RESTEasy JARs to the project if they appear in the root runtime directory but are not installed in the runtime. While this is not required, it will assist when working with JBoss Application Server 5 and JBoss Enterprse Application Platform 5 web service projects.
    Sample Web Service ClassPackageyesEnter the package for the web service class.The default package for the sample web service will be displayed.
     ClassyesEnter the name of the web service class containing the JAX-RS annotated path. This class defines the path to the web service and is referenced in the Application Class Name. The Application Class Name is declared in the deployment descriptor providing indirect access to the annotated path.
     Application Class NameyesEnter the name of the Application Class Name. The Application Class Name constructor instantiates objects of the web service class containing the JAX-RS annotated path, GET and POST methods. It serves as a single point of access to the application for the web server.

  4. Generate the web service

    Click the Finish button to complete the web service setup.

    Result:

    The web service classes will be generated and the web.xml file updated with the deployment details.

  5. Browse the MyRESTApplication.java class

    Double click the MyRESTApplication.java class and note the constructor instantiating objects of type HelloWorldResource. The relevance of this will be discussed shortly.

    Figure 2.4. Application Class - MyRESTApplication.java


  6. Browse the HelloWorldResource.java class

    Double click the HelloWorldResource.java class and note the JAX-RS annotated path and the annotated GET method.

    Figure 2.5. HelloWorldResource.java


  7. Browse the web.xml deployment descriptor

    Double click the web.xml file and note the jax.ws.rs.Application parameter mapped to the Application class. Note also that:

    • the main servlet for the application is org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher which is given the custom name Resteasy; and

    • the main servlet is not mapped to a particular url as indicated by /*.

    The url for sending GET requests can be resolved as follows:

    1. Identify the Application Class as defined in the deployment descriptor.

    2. Note the object type instantiated in the Application class and added to the singleton set: HelloWorldResource.

    3. Note the JAX-RS annotated path declared in the corresponding HelloWorldResource class: @Path("/MyRESTApplication") [1].

    Figure 2.6. web.xml


    The url for sending GET requests is therefore http://localhost:8080/ProjectName/[1] or, http://localhost:8080/RestfulSample/MyRESTApplication.