Lesson 2: Creating and testing the web service

After you create a JAX-RS enabled project, you can download the Java™ files that are used by the application and test the web service.

Create a JAX-RS web service

Procedure

  1. Download the sample XXX. The project that contains the Java classes that are needed for the application.
  2. In your web project, create a package called com.test (right-click Java Resources > src and select New > Package). Import the following classes from the downloaded project into the package:
    • AddressBook.java
    • AddressBookApplication.java
  3. Open WebContent/WEB-INF/web.xml. In the Design view, select the Servlet (JAX-RS Servlet) and click Add and add an Initialization parameter to the JAX-RS servlet, leaving the name and value fields empty. Save web.xml ignoring any errors that might be displayed.
    Screen capture that shows how to add an initialization parameter to the Web application deployment descriptor of your JAX-RS enabled project.
  4. In the Problems view, right-click the param-name warning and select Quick Fix. Select to browse for an existing subclass, and select the AddressBookApplication.
  5. Save web.xml.

Test the JAX-RS web service

Procedure

  1. In the Servers view, right-click your server and select Add and Remove, and add the JAX-RS EAR to the server. Restart the server.
  2. To retrieve all addresses in the Address Book application, open a Web browser and enter the following URL: http://localhost:<default_host_port>/<application_name>/jaxrs/addresses
    For example, following the naming convention used in this tutorial and the default port, http://localhost:9080/JAXRS/jaxrs/addresses
    Note: For WebSphere Application Server traditional, you can determine the default host port name in the WebSphere Application Server Admin Console server configuration tab.
  3. Enter the following URL: http://localhost:<default_host_port>/<application_name>/jaxrs/addresses/<address index>
    The address index is a number in the range 0 - 5 that represents the 6 addresses that are listed in AddressBook.java. The address that is assigned to that index value is displayed.