Monday, November 16, 2009

RESTful Web Services using Eclipse and Jersey

Axis2 wraps everything in XML, and some people prefer Eclipse over Netbeans, but developing a RESTful web services project in Eclipse without Axis is possible even though it is not straight forward. Here are instructions to create a RESTful web services project in Eclipse without Axis:

Setup

  1. Download Eclipse IDE for Java EE Developers (as opposed to Java Developers) from http://www.eclipse.org/downloads/
  2. Download Glassfish from https://glassfish.dev.java.net/downloads/v2.1-b60e.html and follow the instructions on the web page.
  3. Download Jersey 1.1 from https://jersey.dev.java.net/servlets/ProjectDocumentList
  4. Install the Firefox "Poster" add-on

Create the RESTful web service project
  1. Create a new Dynamic Web Project that uses GlassFish v2.x for the Target Runtime
  2. Right-click in the Servers view and create a new GlassFish v2.x server
  3. Create a new package in the Java Resources folder
  4. Create a new Java class in the new package in the Java Resources folder (hereafter referred to as class A)
  5. In the Properties for the project, on the Java Build Path page, in the Libraries tab, click on "Add External JARs"
  6. Find the jsr311-api*.jar in the folder you installed Jersey to, and add it as an external JAR
  7. Add the @Path("insert_a_path_here") annotation right before the declaration of class A
  8. Add a method to class A that has the @GET annotation and the @Produces("application/xml") annotation right before the method declaration
  9. Create another class in the same package as class A (hereafter referred to as class B)
  10. Add the @XmlRootElement(name="insert_a_name_here") right before the declaration of class B
  11. Add some random fields to class B and generate getters and setters for them
  12. Add the @XmlElement annotation from the javax.xml.bind.annotation package right before the getters
  13. Modify class A to return a new instance of class B
  14. Replace the contents of the web.xml file in the WebContent/WEB-INF directory with
  15. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <session-config>
    <session-timeout>
    30
    </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

  16. Right-click on the project and choose Run As->Run on server.
  17. Open the Poster plug-in in Firefox and run a GET request on http://localhost:8080/insert_name_of_dynamic_web_project_here/resources/insert_the_path_specified_in_the_annotation_for_class_A_here (modify port numbers as necessary depending on output of GlassFish application deployment in the "Console" tab)
  18. You should get a XML response with the data from class B
  19. El fin!


Note: if you want to return a list of objects, use the following annotations before the getter that returns the list:

@XmlElementWrapper
@XmlElements({@XmlElement(name="element_name")})

where element_name is "suggestion" in the example XML below:

<suggestions>
<suggestion>a suggestion</suggestion>
<suggestion>another suggestion</suggestion>
</suggestions>

Also note that the web.xml and sun-web.xml contain the two parts of the URL before the part specified by the @Path annotation.