Java Reference
In-Depth Information
REST Response Types: Some feel that the return of anything but hypermedia-
based content is not truly RESTful. However, in our opinion, as long as the system
stays true to the REST principles for the request and the communication protocol,
the response type is unimportant.
When you build a Web application with a Java Servlet, for example, it's straight-
forward to read the data passed through URL query parameters, and to return any
text-based response to the caller. The Java Servlet doPost method implementa-
tion in Listing 10.2 illustrates this. Here, the parameters used in the HTTP query
URL above are read and used to retrieve a user's employee benefits. The results
are encoded as human-readable text. Because this is an example of a RESTful
service, the request can be initiated—and the response viewed—by a Web
browser, or any component in a distributed application.
Listing 10.2
Java Servlet doPost
protected void doPost( HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
ServletOutputStream out = resp.getOutputStream();
String response;
String userSSID = req.getParameter("user");
String userType = req.getParameter("type");
if ( userType.equals("full_time_employee")) {
Employee emp = lookupUser(userSSID);
String medPlan = emp.getMedicalPlan();
String dntPlan = emp.getDentalPlan();
String retPlan = emp.getRetirementPlan();
Response = "User " + emp.getFullName() +
" has medical plan: " + medPlan +
", and dental plan: " + dntPlan +
", and retirement plan: " + retPlan;
}
else {
// ...
}
// Output the response from the worker
out.println(response);
}
For the remainder of this chapter, we're going to focus on consuming RESTful
services from a JavaFX application. In particular, we're going to examine just
 
Search WWH ::




Custom Search