In this article, we will create a standalone Restful web service using Restlet framework and Maven.
If you need help setting up Maven, refer this article :
Maven basics
Let’s create a standalone java application by using the following archetype in command prompt:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.topjavatutorial.app -DartifactId=RestletStandAloneApp -DinteractiveMode=false
Open the project in eclipse or in any other IDE and add following dependency in it :
<dependency> <groupId>org.restlet.jse</groupId> <artifactId>org.restlet</artifactId> <version>2.1-RC2</version> </dependency> <dependency> <groupId>org.restlet.jse</groupId> <artifactId>org.restlet.ext.simple</artifactId> <version>2.1-RC2</version> </dependency>
Also, add this repository :
<repositories> <repository> <id>maven-restlet</id> <name>Public online Restlet repository</name> <url>http://maven.restlet.org</url> </repository> </repositories>
The project should automatically download the dependent jars. You can also force it using mvn compile.
Create Resource
In com.topjavatutorial.app package, create following resource class :
package com.topjavatutorial.app; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; public class HelloWorldResource extends ServerResource { @Get public String getMsg() { return "Hello World"; } }
Create Server
We need to create a Http server connector to make this resource available.
Create a App.java file in same package(unless already created by Maven) and add following code :
package com.topjavatutorial.app; import org.restlet.Server; import org.restlet.data.Protocol; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { System.out.println( "Hello World!" ); // Use any available port Server helloServer = new Server(Protocol.HTTP, 8182, HelloWorldResource.class); helloServer.start(); } }
Test the service
Run the java program and point your browser to :
http://localhost:8182/
You should see :
Hello World
Create Client
Restlet also lets us create client to consume a service.
ClientResource works as a proxy that we can use to run methods on the remote resource.
package com.topjavatutorial.app; import java.io.IOException; import org.restlet.resource.ClientResource; import org.restlet.resource.ResourceException; public class HelloWorldClient { public static void main(String[] args) throws ResourceException, IOException { ClientResource clientResource = new ClientResource("http://localhost:8182/"); clientResource.get().write(System.out); } }
Running this program should produce following output :
Hello World
Here is the structure of the project generated :
Code download from GitHub
https://github.com/TopJavaTutorial/RestletApp.git
You may also like following articles :
Restful webservice with Jersey and Grizzly
Restful webservice with Jersey
Restful webservice with CRUD operations using Jersey and Hibernate
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post