+

Search Tips   |   Advanced Search

Define the resources in RESTful applications

Use Java API for RESTful Web Services (JAX-RS) to develop services that follow Representational State Transfer (REST) principles. RESTful services are based on manipulating resources. Resources can contain static or dynamically updated data. By identifying the resources in the application, we can make the service more useful and easier to develop.

After we have identified the application to expose as a RESTful service, first define the resources for our RESTful application. After we have defined the resources for the application, consider the type of data we want to expose. Perhaps we already have a relational database containing information to expose to users that are using the REST technology. Do we already have a set of Java classes defined for accessing that data?

For example, consider the case of an application that is defined to support a book store. This application currently has a database with several tables that define the various items in the collection of books and the inventory of each book. In this example, there are a number of ways to represent the data in the database in a RESTful application. One approach is to consider each table as an individual resource, so that each of the verbs in the RESTful request maps to the actions that the database supports on that table such as select, insert, update, delete. This example is a simple approach to creating a RESTful application. This approach using the book store example is also used in the documentation that describes defining URL patterns for resources, resource methods, HTTP headers and response codes, media types, and parameters for request representations to resources

In support of this database for the book store application, there might already be existing code that is responsible for accessing the database and retrieving the data from each table. Even though the rows in each of the tables logically represent each resource, the accessor classes are used to define the resources. The implementing JAX-RS applications documentation provides more details on how these classes are incorporated into the JAX-RS application.

Alternately, we might have more static content that does not reside in a database to distribute as resources. Whether it is a collection of documents in various formats or a resource-based facade for other remote systems, when using JAX-RS, we can distribute content from multiple sources.

Resources are the basic building block of a RESTful service. Examples of a resource from an online book store application include a book, an order from a store, and a collection of users.

Resources are addressable by URLs and HTTP methods can perform operations on resources. Resources can have multiple representations that use different formats such as XML and JSON. Use HTTP headers and parameters to pass extra information that is relevant to the request and response.

With JAX-RS, we can annotate existing or new Plain Old Java objects (POJO) with JAX-RS specific annotations. JAX-RS annotated resource classes and the annotated methods are started depending on the URI patterns.


Tasks

  1. Identify the types of resources in the application.
  2. (optional) Identify existing Java classes we can use as resource classes.

  3. Create new Java classes for resources that do not have an existing Java class.

We have defined the content to expose as a collection of resources in the application.


What to do next

Based on the resources that we have defined, read about defining URL patterns for resources, resource methods, HTTP headers and response codes, media types, and parameters for request representations to resources to learn more about additional steps we can take to define the resources for our JAX-RS application.


Related:

  • Overview of IBM JAX-RS
  • Define the URI patterns for resources in RESTful applications
  • Define resource methods for RESTful applications
  • Define the HTTP headers and response codes for RESTful applications
  • Define parameters for request representations to resources in RESTful applications
  • Define media types for resources in RESTful applications
  • Implement JAX-RS web applications