Java Reference
In-Depth Information
private Collection<SeatDto> getSeatsFromServer() {
return seatResource.request().get(new
GenericType<Collection<SeatDto>>() { });
}
private void printSeats(Collection<SeatDto> seats) {
seats.forEach(System.out::println);
}
private void bookSeats(Collection<SeatDto> seats) {
for (SeatDto seat : seats) {
try {
String idOfSeat =
Integer.toString(seat.getId());
seatResource.path(idOfSeat).request().post(Entity.json(""),
String.class);
System.out.println(seat + " booked");
} catch (WebApplicationException e) {
Response response = e.getResponse();
StatusType statusInfo =
response.getStatusInfo();
System.out.println(seat + " not booked (" +
statusInfo.getReasonPhrase() + "):"
response.readEntity(JsonObject.class). getString("entity"));
}
}
}
}
In the highlighted fragments, you can see the REST calls used to retrieve the data and
booking seats. Our post call requires an ID to be specified; we do that by using the
path method of the request builder. It is also possible to make the call asynchron-
ously, using the async method and a Future object:
Future<Collection<SeatDto>> future = seatResource.request()
.async().get(new GenericType<Collection<SeatDto>>()
{});
Search WWH ::




Custom Search