< Previous

Lesson 2: Creating and testing the web service

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

Create a JAX-RS web service

Procedure

  1. Download the project which contains the Java classes 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 showing 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 sub-class, 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: 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 between 0 and 5 which represent the 6 addresses listed in AddressBook.java. The address assigned to that index value will display.
< Previous
Icon that indicates the type of topic Tutorial lesson topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: jaxrs_exercise13.html