Java Reference
In-Depth Information
Accessing REST resources
As we said, REST resources can be accessed using actions that map an equivalent HTTP
request. In order to simplify the development of REST applications, you can use simple an-
notations to map your actions; for example, in order to retrieve some data from your applic-
ation, you can use something similar to the following:
@Path("/users")
public class UserResource {
@GET
public String handleGETRequest() { . . .}
@POST
public String handlePOSTRequest(String payload) { . . . }
}
The first annotation,
@Path
, in our example is used to specify the URI that is assigned to
this web service. Subsequent methods have their specific
@Path
annotation so that you
can provide a different response according to the URI requested.
Then, we have an
@GET
annotation that maps an HTTP
GET
request and an
@POST
an-
notation that handles an HTTP
POST
request. So, in this example, if we were to request for
a web application bound to the
example
web context, an HTTP
GET
request to the URL
http://host/example/users
would trigger the
handleGETRequest
method;
on the other hand, an HTTP
POST
request to the same URL would conversely invoke the
handlePOSTRequest
method.