Rest service Hello World with Restlet


Tools and Technologies used


Helloworld_restlet

Project structure


restlet_hello_world_proj_structure

Step 1

Create a new Maven project with name “RestletHelloWold” in eclipse.

Refer Step 1 in this article for the same.

Step 2

Add the Restlet related dependencies to pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4.         http://maven.apache.org/maven-v4_0_0.xsd">
  5.   <modelVersion>4.0.0</modelVersion>
  6.   <groupId>RestletHelloWorld</groupId>
  7.   <artifactId>RestletHelloWorld</artifactId>
  8.   <packaging>war</packaging>
  9.   <version>0.0.1-SNAPSHOT</version>
  10.   <name>RestletHelloWorld Maven Webapp</name>
  11.   <url>http://maven.apache.org</url>
  12.  
  13.  <dependencies>
  14.       <dependency>
  15.               <groupId>junit</groupId>
  16.               <artifactId>junit</artifactId>
  17.               <version>3.8.1</version>
  18.              <scope>test</scope>
  19.       </dependency>
  20.  
  21.       <dependency>
  22.              <groupId>org.restlet.jee</groupId>
  23.              <artifactId>org.restlet</artifactId>
  24.              <version>2.3.4</version>
  25.       </dependency>
  26.  
  27.      <dependency>
  28.             <groupId>org.restlet.jee</groupId>
  29.             <artifactId>org.restlet.ext.jaxrs</artifactId>
  30.             <version>2.3.2</version>
  31.      </dependency>
  32.  
  33.      <dependency>
  34.            <groupId>org.restlet.jee</groupId>
  35.            <artifactId>org.restlet.ext.servlet</artifactId>
  36.            <version>2.3.4</version>
  37.      </dependency>
  38.  
  39.  </dependencies>
  40.  
  41.   <repositories>
  42.         <repository>
  43.             <id>maven-restlet</id>
  44.             <name>Public  Restlet repository</name>
  45.             <url>http://maven.restlet.org</url>
  46.         </repository>
  47.   </repositories>
  48.  
  49.   <build>
  50.     <finalName>RestletHelloWorld</finalName>
  51.   </build>
  52. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RestletHelloWorld</groupId>
  <artifactId>RestletHelloWorld</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestletHelloWorld Maven Webapp</name>
  <url>http://maven.apache.org</url>
 
 <dependencies>
      <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
             <scope>test</scope>
      </dependency>
  
      <dependency>
             <groupId>org.restlet.jee</groupId>
             <artifactId>org.restlet</artifactId>
             <version>2.3.4</version>
      </dependency>

     <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.jaxrs</artifactId>
            <version>2.3.2</version>
     </dependency>

     <dependency>
           <groupId>org.restlet.jee</groupId>
           <artifactId>org.restlet.ext.servlet</artifactId>
           <version>2.3.4</version>
     </dependency>
 
 </dependencies>

  <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public  Restlet repository</name>
            <url>http://maven.restlet.org</url>
        </repository>
  </repositories>

  <build>
    <finalName>RestletHelloWorld</finalName>
  </build>
</project>

We have defined a maven restlet Repository to download the Restlet related dependencies.


Step 3

Update web.xml file with Restlet servlet container

  1. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  2.                         "http://java.sun.com/dtd/web-app_2_3.dtd" >
  3.  
  4. <web-app>
  5.   <display-name>Archetype Created Web Application</display-name>
  6.   <context-param>
  7.         <param-name>org.restlet.application</param-name>
  8.         <param-value>com.kb.rest.MyRestletApplication</param-value>
  9.  </context-param>
  10.  
  11.  <servlet>
  12.      <servlet-name>RestletServlet</servlet-name>
  13.      <servlet-class>
  14.              org.restlet.ext.servlet.ServerServlet
  15.      </servlet-class>
  16.  </servlet>
  17.  
  18.  <servlet-mapping>
  19.      <servlet-name>RestletServlet</servlet-name>
  20.      <url-pattern>/rest/*</url-pattern>
  21.  </servlet-mapping>
  22. </web-app>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
        <param-name>org.restlet.application</param-name>
        <param-value>com.kb.rest.MyRestletApplication</param-value>
 </context-param>

 <servlet>
     <servlet-name>RestletServlet</servlet-name>
     <servlet-class>
             org.restlet.ext.servlet.ServerServlet
     </servlet-class>
 </servlet>
 
 <servlet-mapping>
     <servlet-name>RestletServlet</servlet-name>
     <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>

We have defined the context param to point the application class where the Restlet servlet has to look for any resource mapping class.

In our case, it is MyRestletApplication class which will assist the Restlet service to find the mapping resource.

Step 4

Create the MyRestletApplication class

  1. package com.kb.rest;
  2.  
  3. import org.restlet.Application;
  4. import org.restlet.Context;
  5. import org.restlet.Restlet;
  6. import org.restlet.routing.Router;
  7.  
  8. public class MyRestletApplication extends Application {
  9.    
  10. public MyRestletApplication(Context parentContext) {
  11.     super(parentContext);
  12.   }
  13.  
  14.   @Override
  15.   public  Restlet createInboundRoot() {
  16.     Router router = new Router(getContext());
  17.     router.attach("/hello", HelloWorldRestService.class);
  18.     return router;
  19.   }
  20. }
package com.kb.rest;

import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class MyRestletApplication extends Application {
	
public MyRestletApplication(Context parentContext) {
    super(parentContext);
  }

  @Override
  public  Restlet createInboundRoot() {
    Router router = new Router(getContext());
    router.attach("/hello", HelloWorldRestService.class);
    return router;
  }
}

This class maps the request URL to appropriate Rest service class.

In our case, it is “/hello” which is mapped to HelloWorldRestService class.

Step 5

Create the Rest service class

  1. package com.kb.rest;
  2.  
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.MediaType;
  6.  
  7. import org.restlet.resource.Get;
  8. import org.restlet.resource.ServerResource;
  9.  
  10. @Path("/hello")
  11. public class HelloWorldRestService extends ServerResource {
  12.  
  13.     @Get
  14.     @Produces(MediaType.TEXT_PLAIN)
  15.     public String getMessage(){
  16.         return "Restlet Hello World";
  17.        
  18.     }
  19. }
package com.kb.rest;

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

@Path("/hello")
public class HelloWorldRestService extends ServerResource {

	@Get
	@Produces(MediaType.TEXT_PLAIN)
	public String getMessage(){
		return "Restlet Hello World";
		
	}
} 

@Path – to specify the root path of the resource which is “/hello”

@GET – It’s the HTTP method that is supported by the service

@Produces – It’s used to specify the MIME type that service is supporting which is Plain Text here.

Step 6

Build and Deploy the project in the Tomcat server

Step 7

Start the server

Step 8

Access the Rest service using below URL

http://localhost:8080/RestletHelloWorld/rest/hello

restlet_hello_world_output

Download this project RestletHelloWorld.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on