Introduction
What are RESTfull or Rest web services and how can they be implemented in with Java? Also what is JSON and how to make a webservice that returns a JSON object? This is what this blog is about. Specifically, first, about installing and running a sample RESTfull application available from Jersey project.
What are RESTfull webservices? See
this article. Or any other; there are many.
Approach 1
This approach uses one of the samples provided by the Jersey project and uses Maven to install the libraries. It's a bit heavy approach, but it works. Approach 2 below also uses Jersey, but is way lighter and simpler.
Server Side
I'm using Eclipse IDE with Glassfish Application Server. The steps for this configuration are:
- Add Jersey and Jersey Documentation and Examples components to Glassfish using its admin console. Note, use GlassFish v3 Prelude version. When I used GlassFish v3 Promoted Build, the Examples module was not showing in its Update Tool.
- Restart the GF server.
- Find the Jersey samples in GF_Home/glassfish/jersey folder. Go into helloworld-webapp folder. You should see a .pom file there, that is used by maven. From that folder, run "mvn clean package" command. Install maven if needed.
- Maven installs many files, and for me it ended with an error saying it can't read the glassfish-embedded-all-3.0-Prelude-Embedded-b10.jar file. I have located this file in the maven repository (Documents and Settings/user/.m2/...) and found that it was broken. I had to download it manually from internet (search by the file name).
- Rerun the mvn command. It finished successfully and created a war file in the target directory.
- Open GF console, and from Web Applications do a deploy. Point to the above war file and click OK. That did it.
- Point your browser to http://localhost:8080/helloworld-webapp/helloworld. The string "Hello World" appeared.
Analyzing the Sample
After I had the sample running, I have exploded and analyzed the contents of the generated war file. The file had a class file implementing the resource plus two more short config files.
sun-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
<context-root>/helloworld-webapp</context-root>
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</sun-web-app>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.sun.jersey.samples.helloworld.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Approach 2
Approach 1 worked OK, but I wasn't able to apply the sample to deploy an application of my own. And so I searched for a tutorial that uses Eclipse and possibly no Maven. I have run into this mini
tutorial which worked great. I was able to drop this application into a Tomcat 6.0 and GlassFish 3 Prelude containers and it worked in both fine.
Also, this example illustrates how the service can be implemented for different response formats, leaving to the requestor to decide which one to use.
JSON
JSON is simply a format, in a way an alternative to XML, that is especially useful for (de-)serializing objects, for example in order to send them over internet. The format is derived from JavaScript native format, but it became language neutral. Here's a sample:
{"name":"Alfred Alfredo","age":"47"}
A nice and concise description of JSON syntax is on JSON
website. To have your RESTfull webservice return a JSON object, download and add to the project jettison.jar that Jersey uses to create a JSON object. Now, for the sake of the demonstration, say that the webservice need to return a Person object:
@XmlRootElement
public class Person {
public String name;
public int age;
public Person() {} // required by JAXB
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Thanks to the annotations in this class, in order to return a JSON object we just need to define a method:
@Path("/person")
public class PersonResource {
@GET @Produces("application/json")
public Person get() {
return new Person("Alfred Alfredo",47);
}
}
The JSON object as specified above, in the first part of this section will be returned. You can test it using a browser by pointing it to http://localhost:8080/jersey1/rest/person, where
jersey1 is the context of my web app,
rest is my servlet mapping and
person is what I defined with @Path in my RESTful resource class.