Java Reference
In-Depth Information
Consuming our REST service
Connecting to a RESTful web service takes no more work than directly connecting to
the service through an HTTP connection. For this reason, you can use plenty of APIs
to access your REST services, such as the JDK
URLConnection
class or Jakarta
Commons HttpClient API. However, since we are already using RESTEasy in our
project, we suggest using its great client API that helps you to easily consume REST
web services in the way JAX-RS is consumed.
A simpler way to create an individual HTTP request is using the
org.jboss.resteasy.client.ClientRequest
class. Therefore, if you want to
retrieve the list of
Seats
from your REST service, you just need to issue the follow-
ing
ClientRequest
class:
ClientRequest cr = new
ClientRequest("http://localhost:8080/
ticket-agency-ws/rest/seat");
String result =
cr.get(String.class).getEntity();
System.out.println(result);
The previous code will simply perform a
GET
action to the REST service that is
deployed as part of the
ticket-agency-ws
web application. Once that
Clien-
tRequest
class is returned, we cast the result as a string, which in our case will be
a list of JSON-formatted
Seat
objects.
Here's a quick recap of what the
ClientRequest
class is made up of:
As you can see, besides the
Web Context
string, you need to include the JAX-RS
application path (
rest
) we discussed earlier. Next, you need to include the
@Path
annotation of your
TicketRESTService
(the seat) and eventually the ID of the seat
booking if you are trying to book a seat.