RESTful Web Services in 60 Seconds
Is this even possible? Yes, trust me, if I can do it, so can you. You may ask, from Groovy and Grails in one day, how did I shift gears to web services. It all started while I was reading and working the samples from chapter 9 Web Services of “Beginning Groovy and Grails from Novice to Professional“. With the help of the sample provided in the book, I was able to get a RESTful web services for the Grails application.
However, I wanted to learn more about the same and write a simple RESTful web service in Java. It is traditional to start with a Hello World example while learning any new technology, right? So, lets not break the tradition and continue with the Hello World example I tried.
To follow this tutorial, you need the following software and resources.
1. NetBeans IDE 6.x, I had the latest 6.5 M1 version downloaded.
2. JDK version 5 or 6
3. GlassFish V2 Application Server
4. Last but not the least, knowledge about REST. Here are some links to get you started:
- RESTful Web Services
- Implementing RESTful Web Services in Java
- JSR 311: JAX-RS: The JavaTM API for RESTful Web Services
Creating a New Project:
1. Choose File -> New Project. Select Web within the Categories and select Web Application under Projects and click Next.

2. In the next screen, enter the project name as “HelloWorldRestWS”. Leave the rest as default as shown below:
.
3. In the screen shown below, select GlassFish V2 as the server, Java EE 5 as the Java Version, and HelloWorld as the context path and click Finish.

Creating a Web Resource:
Now, that we have our web application, lets create a simple Java Class for our web resource. To do this:
1. Right click on the project node, and select New - RESTful Web Services from Patterns… This will bring up a wizard as shown below, select the Singleton Pattern and click Next.

2. Next, specify all the details required for the Resource class and click Finish. Don’t forget to change the MIME Type from application/xml to text/plain.

3. At this point, take a look at the source code generated by the IDE, which looks like:
/*
* HelloWorldResource
*
* Created on July 23, 2008, 10:13 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.stelligent.ws;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.ConsumeMime;
/**
* REST Web Service
*
* @author meerasubbarao
*/
@Path("helloWorld")
public class HelloWorldResource {
@Context
private UriInfo context;
/** Creates a new instance of HelloWorldResource */
public HelloWorldResource() {
}
/**
* Retrieves representation of an instance of com.stelligent.ws.HelloWorldResource
* @return an instance of java.lang.String
*/
@GET
@ProduceMime("text/plain")
public String getText() {
//TODO return proper representation object
throw new UnsupportedOperationException();
}
/**
* PUT method for updating or creating an instance of HelloWorldResource
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@PUT
@ConsumeMime("text/plain")
public void putText(String content) {
}
}
Testing the RESTful Web Services:
1. Let’s try this application and see first hand if it works. Right click on the project node and select “Test RESTful Web Services“. The GlassFish V2 application server starts, our web application is deployed, and at this point you should see a link for the web service as shown below, choose the GET method to test.

2. Oops, something went wrong, Internal Server Error?

3. No problem, we can fix this. Let’s fix our getText method which is the culprit for our error.
It was:
/**
* Retrieves representation of an instance of com.stelligent.ws.HelloWorldResource
* @return an instance of java.lang.String
*/
@GET
@ProduceMime("text/plain")
public String getText() {
//TODO return proper representation object
throw new UnsupportedOperationException();
}
Let’s change it to:
/**
* Retrieves representation of an instance of com.stelligent.ws.HelloWorldResource
* @return an instance of java.lang.String
*/
@GET
@ProduceMime("text/plain")
public String sayHello{
//TODO return proper representation object
return "Hello World from REST web services generated in 60 seconds";
}
4. At this point, run the application again and you should get a valid response back.

That’s it. We were able to successfully create, deploy and test RESTful web services. Give it a try, it is just a matter of 60 seconds.

Just yesterday, I posted a detailed review of the book “
The next thing that makes this book so unique is the tests written for each of these frameworks. The authors have free source code download, and you can learn how to write tests using 






