How to setup a Servlet based Restlet Webservice?

If you are starting out the first time with Restlet, then you’re at the right place for tips.

First steps – Head to and download the latest version of the library. I use Restlet 2.0 milestone 6. Milestone releases are not stable tested releases, but they do work quite well.  To create a servlet based Restlet service, download the Java EE zip archive. Once downloaded, unzip it.

This tutorial is for Restlet 2.0 m6 on Eclipse IDE.  The general concept is the same in all IDEs,  you will have to look for similar menu options in other IDEs.

Ok, lets get our hands dirty…

1. Create a new Dynamic Web Project by selecting file->New->Project.

2. Open your My Computer window and navigate to your new project’s folder. Open WebContent folder. Now open Web-INF folder , and inside this open the lib folder. This will be empty at this point. Keep this window open.

3. Open the folder containing the restlet  jar files downloaded from the website. In this, navigate to the lib folder and copy paste the following jar files into the /WEB-INF/lib folder of your web project that you just created in Eclipse –




4. Let’s start with the core of a REST application: the Resource. Here is the code of the single resource defined by the sample application. Copy/paste the code in your “HelloWorldResource” class.

package firstSteps;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
* Resource which has only one representation.
public class HelloWorldResource extends ServerResource {
public String represent() {
return "hello, world";

For more information see Note 2 below.

5. Then, create the sample application. Let’s call it “FirstStepsApplication” and copy/paste the following code

package firstSteps;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
public class FirstStepsApplication extends Application {
* Creates a root Restlet that will receive all incoming calls.
public synchronized Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());
// Defines only one route
router.attach("/hello", HelloWorldResource.class);
return router;

6. Now copy paste the following into the “web.xml” file, this can be found in the folder WebContent/WEB-INF/web.xml

<!-- Application class name -->

<!– Restlet adapter –>
<!– Catch all requests –>

For more info see Note 5 below.

7. Thats it. Now  Run the application by clicking the run button or hitting Ctrl+F11 and type the following in the browser’s address bar – http://<your server name>:<its port number>/firstStepsServlet/hello . Most likely this will be http://localhost:8080/firstStepsServlet/hello when you use tomcat server. If you want to change your server’s port number, refer to this article

Note 1:-  I assume that Tomcat is being used as the webserver running on port 8080 and that its already been installed into your eclipse IDE.

Note 2:- When you use the annotation @Get it means that the function following it should handle the GET request. So it also means that the name of the function can be anything that suits your convenience. And this applies to all the other four annotations as well.

Note 3:- There are totally four types of request that can be handled and the annotations for those four methods are @Get,@Post,@Put,@Delete.

Note 4: – As far as the @Get annotation goes, we can go one step further and say something like this –

public class HelloWorldResource extends ServerResource {
public String represent() {
return "hello, world";


public class HelloWorldResource extends ServerResource {
public String represent() {
return "hello, world";

This means that the function represent will give out a response of the type specified in the brackets.To add  support for multiple types, we can specify them using a ‘pipe’ symbol in between like this –

public class HelloWorldResource extends ServerResource {
public String represent() {
return "hello, world";

To know the list of types that can support and the shortcodes for them see this –

This can be extended to the @Post annotation as well.

Note 5:- The URL pattern in the web.xml contains “/*” as the value. This is the same value for all applications. It means that any request to this application should be redirected to the Class file mentioned above the url pattern in the web.xml. So if I create  Restlet service projects called First_Restlet and Second_Restlet, the web.xml of both the projects will contain the url pattern of “/*”. Even when they get deployed together, the requests will flow to the corresponding ApplicationClasses because the above mentioned urlpattern is tightly coupled,or highly connected to the name of the class file mentioned in the corresponding web.xml.

So http://localhost:8080/First_Restlet/hello and http://localhost:8080/Second_Restlet/hello will map to the respective resources.

About vatsalad

Hi, I'm Vatsala
This entry was posted in How To..., j2ee, java and tagged , , , . Bookmark the permalink.

20 Responses to How to setup a Servlet based Restlet Webservice?

  1. Pingback: Restlet Server: OSGi and Maven « Knowledge Networks

  2. Jon says:

    it doesnt work for me. ¿Dont you need a Im a newbie and i am very confused.

  3. Jon says:

    First of all I would like to thanks you your interest.

    I developed a Jsp web application using Google app engine. It works fine. I use Jdo database included in app engine. I am newbie in web development, and much more about GAE. I think its not necessary to use GWT for using Restlet. Am I right?

    Now i want to contact that Jdo database with an android phone and use that data. I read that the best idea is to use a Reslet Servlet. I followed all the steps of your blog. When I write\hello I receive and error message using my local server.

    Internal Server Error

    If I deploy my application to google app engine and i write I receive a different error.

    Internal Server Error

    If you know how to solve it i would be very grateful because i want to finish my studies as soon as possible and i am really locked with this issue of the Restlets.

    Thank you very much

  4. jon says:

    it started a new project and it works fine. Thank you very much!

  5. Randy says:

    So i am trying to go through this example (thanks BTW)
    but i am stuck
    I am getting this, and not sure where to find it.

    java.lang.NoClassDefFoundError: com/google/gwt/user/server/rpc/SerializationPolicyProvider


  6. kayaman says:

    As i started my programm in eclipse on my windows pc, it was working fine, but when i uploaded it on my virtual server with arch linux i got the 404 error.

    So first of all make sure your Programm directory has the proper privileges. After this i saw my Programm in the Manager Tool… started, but still gave me the 404 finger.

    my last error i found to make it work was the directory:

    Wrong (if you dont use eclipse):


    took me quite some time

    hope i can help sb


    • vatsalad says:

      thanks for sharing this, will make a note.

    • saravanan says:


      I have created my very first project, as you mentioned above, the project is working fine with my Eclipse IDE, but when i deploy it in Linux server, it is showing 404 error.

      It is a J2ee project.

      This is my project structure, Since i’m quite new, i was totally confused.
      Please suggest me what to do, and how should i arrange, also do i need to place the .java files inside webInf.

  7. Tab says:

    Hi Vatsala,

    I want to know how to create a service for retrieving table entities using restlet odata. Please do you have any info?

    Regards ,

  8. Donald says:

    Just to be clear, the URL for the restlet should be something like http://localhost:8080/eclipse-project-name/hello (so, if you named your eclipse project tomcat-restlet-test, like I did, the URL actually turns out to be http://localhost:8080/tomcat-restlet-test/hello)

    Hope this saves some people some time, even though I’m following up to a nine-month-stale thread.

  9. aBeginer says:

    Great tutorial! One thing that tripped me initially was that ‘firstStepsServlet’ is the context root of the web application you create in Eclipse. If you use ‘/’ the URL to test this would just be http://localhost:8080/hello

  10. JP says:

    i am having an issue and came across this example … everything i have is pretty much the same. This works in Internet Explorer 8, but not in Chrome or FireFox … get 404 …. have you tried other browsers?

    • JP says:

      of course, the next link i try explains the error and a work around. Basically need a constructor with the CLAP protocol specified. So in the example above, the FirstStepsApplication class would require:

      public FirstStepsApplication ()
      setOutboundRoot(new Client(Protocol.CLAP));

      Explanation here:

    • vatsalad says:

      Hi JP
      When I was working on this, I was actively using only Chrome and Firefox and It was working fine. Take a look again. Meanwhile let me set this up and check.

  11. sambit says:

    This does not work for me my uri – http://localhost:8080/firstSteps/hello —404 error
    could u plz throw some light ..i am new bie

  12. Pingback: My-Pedia | My learning notes on Restlet

  13. Sha says:

    This does not work for me.
    I am using below URL
    but nothing shows up….just blank error nothing.

    Not sure where am i missing.
    Can you help me Vastala.

Comments are closed.