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
- Download the sample XXX. The project that contains the Java classes that are needed for the application.
-
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.javaAddressBookApplication.java
-
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. Saveweb.xmlignoring any errors that might be displayed.
-
In the Problems view, right-click the
param-namewarning and select Quick Fix. Select to browse for an existing subclass, and select the AddressBookApplication. -
Save
web.xml.
Test the JAX-RS web service
Procedure
- 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.
-
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/addressesFor example, following the naming convention used in this tutorial and the default port,http://localhost:9080/JAXRS/jaxrs/addressesNote: For WebSphere Application Server traditional, you can determine the default host port name in the WebSphere Application Server Admin Console server configuration tab. -
Enter the following URL:
http://localhost:<default_host_port>/<application_name>/jaxrs/addresses/<address index>Theaddress indexis 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.