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
- Download Eclipse IDE for Java EE Developers (as opposed to Java Developers) from http://www.eclipse.org/downloads/
- Download Glassfish from https://glassfish.dev.java.net/downloads/v2.1-b60e.html and follow the instructions on the web page.
- Download Jersey 1.1 from https://jersey.dev.java.net/servlets/ProjectDocumentList
- Install the Firefox "Poster" add-on
Create the RESTful web service project
- Create a new Dynamic Web Project that uses GlassFish v2.x for the Target Runtime
- Right-click in the Servers view and create a new GlassFish v2.x server
- Create a new package in the Java Resources folder
- Create a new Java class in the new package in the Java Resources folder (hereafter referred to as class A)
- In the Properties for the project, on the Java Build Path page, in the Libraries tab, click on "Add External JARs"
- Find the jsr311-api*.jar in the folder you installed Jersey to, and add it as an external JAR
- Add the @Path("insert_a_path_here") annotation right before the declaration of class A
- Add a method to class A that has the @GET annotation and the @Produces("application/xml") annotation right before the method declaration
- Create another class in the same package as class A (hereafter referred to as class B)
- Add the @XmlRootElement(name="insert_a_name_here") right before the declaration of class B
- Add some random fields to class B and generate getters and setters for them
- Add the @XmlElement annotation from the javax.xml.bind.annotation package right before the getters
- Modify class A to return a new instance of class B
- Replace the contents of the web.xml file in the WebContent/WEB-INF directory with
- Right-click on the project and choose Run As->Run on server.
- 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)
- You should get a XML response with the data from class B
- El fin!
<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>
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.