Archive

Archive for the ‘REST/ROA’ Category

Restlet with Java SE

There are three ways to use Restlet within Eclipse IDE. The first one is to create a Java project and use the Restlet JARs as external libraries. Download the latest stable Java SE edition here and extract the archive, i.e. restlet-jse-2.0.9.zip. Add the library in the lib folder in your project properties.

The second way is to install Restlet JARs as Eclipse/OSGi bundles. All Restlet JARs including dependencies are valid OSGi bundles, so this is very convenient if you work in an Eclipse plug-in environment, such as an Eclipse RCP application. You then just need to create a plug-in project. Have a look at the FAQs here for more informations.

My prefered way is to use Maven. Install Maven and create an eclipse project. Then create a new java class MyServerResource and insert the following code:

import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

public class MyServerResource extends ServerResource {

	public static void main(String[] args) throws Exception {
		// Create the HTTP server and listen on port 8182
		new Server(Protocol.HTTP, 8182, MyServerResource.class).start();
	}

	@Get
	public String toString() {
		return "hello, world";
	}
}

Then add the Restlet repository to your pom.xml:

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

And the dependency:

...
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
...

Then run your project and open http://localhost:8182 in your a Web browser.

Advertisements
Categories: REST/ROA