Java Reference
In-Depth Information
@POST
@Path("/{id:\\d+}")
@Produces(MediaType.APPLICATION_JSON)
public Response buyTicket(@PathParam("id")
int id) {
Response.ResponseBuilder builder = null;
try {
service.buyTicket(id);
builder = Response.ok("Ticket booked");
}
catch (Exception e) {
// Handle generic exceptions
Map<String, String> responseObj = new
HashMap<String, String>();
responseObj.put("error", e.getMessage());
builder =
Response.status(Response.Status.BAD_REQUEST).entity(responseObj);
}
return builder.build();
}
}
If you have understood our earlier section well, this code will be almost intuitive to
you. We have included two methods here, just like the SOAP alter ego; the former
one is named getSeatList , which is bound to an HTTP GET request and produces
the list of Seats . The list is returned using a JSON representation that is pretty com-
mon when returning Java objects to the client.
Note
The grammar for JSON objects is simple and requires the grouping of the data
definition and data values, it is as follows:
• Elements are enclosed with curly brackets ( { and })
Search WWH ::




Custom Search