A RESTful Web Service as a JAX-RS Resource
The servlet API, the grizzled workhorse for producing Java websites, is still nimble enough to support RESTful web services as well. There are more recent APIs, among them JAX-RS (Java API for XML-RESTful Services). JAX-RS relies upon Java annotations to advertise the RESTful role that a class and its encapsulated methods play. Jersey is the reference implementation (RI) of JAX-RS. RESTEasy, a JBoss project; Apache Wink; and Apache CXF are other implementations. JAX-RS has APIs for programming RESTful services and clients against such services; the two APIs can be used independently. This section focuses on the service-side API. The first JAX-RS example supports only GET requests, but the second JAX-RS example supports all of the CRUD operations.
A First JAX-RS Web Service Using Jersey
JAX-RS web services are resources
that can be published with the Tomcat and Jetty web servers. The first example has
one resource, the class Adages
, and two supporting Java classes: the
deployment class RestfulApplication
and the POJO class Adage
. Exactly how these three classes
interact is covered next.
The RestfulAdage
class (see Example 2-5) extends the
JAX-RS Application
class (line 2), which implements a getClasses
method that enumerates the individual resources
deployed in the WAR file (line 3). In this example, there is but one such resource, Adages
, but there
could be arbitrarily many (line 4).
Example 2-5. The JAX-RS Application
subclass that lists Adages.class ...
Get Java Web Services: Up and Running, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.