Using Restlet REST with Codenvy

Create a temporary workspace with a Restlet project using a Codenvy Factory:

Restlet is a lightweight, comprehensive, open source REST framework for the Java platform. Restlet is suitable for both server and client Web applications. It supports major Internet transport, data format, and service description standards like HTTP and HTTPS,SMTP, XML, JSON, Atom, and WADL.

Create a Project

Login to your codenvy workspace and create a New WAR Project say “restlet”. Project file structure is shown below:

GetImage (16)

Add Dependencies

Specify the below dependencies in pom.xml which are prerequisites. Make sure these jars are included under Maven Dependencies.

GetImage (17)

Pom.xml :

<dependencies>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.org</url>
</repository>
</repositories>

Create HelloWorldResource Class :

In this example, GET request is accepted/allowed.
We declare a Resource which has only one representation.
Code Logic :

package com.codenvy;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

public class HelloWorldResource extends ServerResource {

@Get
public String greet() {
return "Hello World!";
}
}

Create HelloWorldApplication class

Here we create a root Restlet that will receive all incoming calls. Declare a router Restlet that routes each call to a new instance of HelloWorldResource. Also we define only one route (/hello). We state this class in web.xml as Application Class Name under param-name [ org.restlet.application ].

package com.codenvy;

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

public class HelloWorldApplication extends Application {

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

Create web.xml:

<web-app>
<display-name>Restlet Tutorial</display-name>
<!-- Application class name -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>
com.codenvy.HelloWorldApplication
</param-value>
</context-param>
<!-- Restlet adapter -->
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>
org.restlet.ext.servlet.ServerServlet
</servlet-class>
</servlet>
<!-- Catch all requests -->
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Upon successful building the application and navigating to url “projectURL/hello”, it displays.

GetImage (18)