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.

July 24th, 2008 at 1:15 pm
Test Early » RESTful Web Services in 60 Seconds…
Test Early » RESTful Web Services in 60 Seconds…
July 26th, 2008 at 12:12 am
Great tutorial. I caught one exception though…you need to add the parentheses to the sayHello method declaration or else the example will not work.
Tested and verified with 6.5 M1 and the latest build as of 7/26/2008.
July 26th, 2008 at 9:06 am
Hi Josh,
Thanks for the same. It was a typo while writing this post.
October 15th, 2008 at 9:25 am
Why create such a tool when some existing tools like SOAPbox from Vordel or other provide lot of functionalities ?
Web services are very complicated and security is very important and need to be tested.
November 3rd, 2008 at 4:34 am
I am desperately looking for sample webservice application
configured on Eclipse.Could u plz help….
November 18th, 2008 at 4:18 pm
Meera — it’s not often these “(technology-de-jour) in 60 seconds” type tutorials actually deliver the goods, but yours does. A great way to get going with NetBeans, the JDK and Glassfish, all of which I had extremely limited knowledge up until today. I still have much to learn, but your tutorial was a great way to bootstrap into the Java landscape. Thanks!
December 9th, 2008 at 4:48 pm
I followed your tutorial and I get the following error message: “Cannot access WADL: Please restart your RESTful application, and refresh this page.”
I have researched this and haven’t come up with any solutions.
March 6th, 2009 at 7:23 pm
This was a nice tutorial. Actually took 60 seconds to write my first Restful Web Services example.
April 1st, 2009 at 12:36 am
Hello,
is there sample code for creating RESTful Web Services in VS 2008 .net 3.5?
thanks
Rasik Godehani
June 9th, 2009 at 5:24 am
Hi, Can any one post a bit complicated REST service and how to implement.
June 12th, 2009 at 6:00 am
This is really great. Thanks a lot for this quick guide. Helped me get started with writing my own first rest service .
Just the stuff I needed.
June 25th, 2009 at 10:27 pm
Really great way to get started with Net Bean, I’ve always thought Eclipse was the answer to everything..but I guess not.
Anyways, the fact that everything is tied into Net Bean could make you look silly when you go to Eclipse where you have to do your own server, database and web services configuration.
I’d like some help from anyone who has a good understanding of how to build and deploy REST web services in eclipse using any of the opened source app servers[Tomcat, jboss etc)
July 20th, 2009 at 2:43 pm
Nice quicky page.. Thanks