COMP 655: Distributed/Operating Systems - Summer 2011
2024-03-28 20:21:08 UTC
Home
Lectures
Assignments
Books, web, tools
 
Turnitin.com
Guidelines
Writing help
Plagiarism
 
DiNo
Glassfish
RESTful client
Menu service
JAX-RS
JAXB
EJB
Java
 
Bulletin board
Contact
 JAX-RS
JAX-RS is an API for implementing RESTful services as Java Servlets. It supports URI parsing, matching service capabilities with the data representation(s) supported by the client, and creating responses, among other things. JAX-RS is defined by JSR 311.

JAX-RS is strictly an implementation technology; clients should not be able to detect whether a service uses it.

Four implementations are available:

Because of its compatibility with GlassFish, I recommend using Jersey if you choose to use JAX-RS in 655. 

Remember that JAX-RS is still fairly new and still a moving target. 


Writing a servlet with Jersey vs writing it directly

Jersey supplies the Servlet class: In the first lab, you created a TinyServlet by using Eclipse features to create a servlet that extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet. If you use Jersey, then Jersey supplies the servlet for you. It's a general-purpose servlet that knows how to search your code for classes and methods that carry the JAX-RS annotations. For example, when the client sends a PUT request, the doPut() method in the Jersey sevlet gets called by the container, then it calls the appropriate method (if any) from one of your classes with an @PUT annotation on it. Jersey's appropriate-ness check matches up the request's URI with your @Path annotations, and the request's Content-Type and Accepts headers, if any, with your @Consumes and @Provides annotations, if any. Bottom line: your code should NOT include a Servlet (just as MenuResource in the menu service is not a Servlet)

Specify the Jersey servlet in web.xml: for the tiny servlet, the defaults that Eclipse put in the web.xml file worked fine. If you're using Jersey for your homework or DiNo, however, you'll need a web.xml that specifes the Jersey Servlet (see the web.xml in the menu service reference implementation or the Java homework reference implementation).

With Jersey, there's one servlet per web app: Actually, I don't know if this is a requirement with Jersey, or just the norm. I've never tried. You don't need more than one servlet, because you can supply as many JAX-RS-annotated classes as you want. If you're writing servlets directly, you can use more than one servlet in a web app. You list all of the servlets in your web.xml, and use url-pattern elements to divide up the request URI space among the servlets. For example, if you had some URIs that start with /a and others that start with /b, you could write ServletA and ServletB, and associate ServletA with the url-pattern /a/* and ServletB with the url-pattern /b/*