Can any one give me an example of Java Restlet?
**Helloworld Example in Restlet with the use of Json.**
This example is created for HTTP GET and POST
Step 1 : Configure Restlet to Project
to configure restlet, we need Restlet Libraries and JSON libraries to enable restlet Feature.
Please configure below libraries to project
Step 1.2: configure RestletServlet in Web.xml
<servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> <init-param> <param-name>org.restlet.application</param-name> <param-value>com.test.jp.application.HelloWorldApplication</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
Here in
Whenever any request comes with /service/* url it will automatically forwarded to HelloWorldApplication class.
Step 2: Create HelloWorldApplication Class.
Create helloWorldApplication.java in com.test.jp.application package
Sample Code of HelloWorldApplication.java
/** * */ package com.test.jp.application; import org.restlet.Application; import org.restlet.Restlet; import org.restlet.routing.Router; import com.test.jp.resource.HelloWorldResource; // /** * @author @JP@ * */ public class HelloWorldApplication extends Application { @Override public synchronized Restlet createInboundRoot() { System.out.println("Called Application "); Router router = new Router(); router.attach("/helloworld",HelloWorldResource.class ); return router; } }
in this class we have to override the createInboundRoot() method. In createInboundRoot() method Router class is used. router.attach("/helloworld",HelloWorldResource.class)
here we have attached /helloworld URI with the HelloWorldResource class. Whenever any request fired with /service/helloworld, it will automatically forwared to HelloWorldResource class.
Step 3: Create HelloWorldResource.java
we have to create HelloWorldResource.java class in com.test.jp.resource package.
Sample Code:
/** * */ package com.test.jp.resource; import java.util.LinkedHashMap; import java.util.Map; import org.json.JSONObject; import org.json.simple.JSONValue; import org.json.simple.parser.JSONParser; import org.restlet.data.MediaType; import org.restlet.ext.json.JsonRepresentation; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; import org.restlet.resource.Post; import org.restlet.resource.ServerResource; /** * @author @JP@ * */ public class HelloWorldResource extends ServerResource { @Get public Representation printHelloWorld(Representation entity) { System.out.println("Called Method"); String jSonStr = ""; try { /*JsonRepresentation represent = new JsonRepresentation(entity); JSONObject jObject = represent.getJsonObject(); JSONParser jParser = new JSONParser(); String jString = jObject.toString(); Map<Object,String> jSon = (Map) jParser.parse(jString); System.out.println(";;-->>> GREETINGS;;->" + jSon.get("greeting")); */ LinkedHashMap<Object, String> list = new LinkedHashMap<Object, String>(); list.put("service_status", "true"); list.put("system_status", "true"); list.put("result", "Hello World"); jSonStr = JSONValue.toJSONString(list); return new StringRepresentation(jSonStr, MediaType.APPLICATION_JSON); } catch(Exception ex) { ex.printStackTrace(); LinkedHashMap<Object, String> list = new LinkedHashMap<Object, String>(); list.put("service_status", "true"); list.put("system_status", "true"); list.put("result", "Problem in Sending Greeting"); jSonStr = JSONValue.toJSONString(list); return new StringRepresentation(jSonStr, MediaType.APPLICATION_JSON); } } @Post("json") public Representation printGreetings(Representation entity) { System.out.println("Save Greetings"); String jsonString =""; try { JsonRepresentation represent = new JsonRepresentation(entity); JSONObject jObject = represent.getJsonObject(); JSONParser jParser = new JSONParser(); String jString = jObject.toString(); Map jSon = (Map) jParser.parse(jString); System.out.println(";;-->>> GREETINGS;;->" + jSon.get("greeting")); LinkedHashMap list = new LinkedHashMap(); list.put("service_status", "true"); list.put("system_status", "true"); list.put("result", jSon.get("greeting")); jsonString = JSONValue.toJSONString(list); return new StringRepresentation(jsonString, MediaType.APPLICATION_JSON); } catch(Exception ex) { LinkedHashMap<Object, String> list = new LinkedHashMap<Object, String>(); list.put("service_status", "true"); list.put("system_status", "true"); list.put("result", "No Data Found!"); jsonString = JSONValue.toJSONString(list); ex.printStackTrace(); return new StringRepresentation(jsonString, MediaType.APPLICATION_JSON); } } }
in HelloWorldResource class.
We have to create two methods for HTTP GET and HTTP POST
in printHelloWorld() method HTTP GET is configured.. Whenever GET request came. This method is automatically called. We haven't pass any arguments from JSP client side that's why JSON Code is commented in that method.
In printGreetings() method HTTP POST is configured. In this method I am passing Greetings using JSON
STEP 4: configured TOMCAT
STEP 5: configure RESTClient in mozilla firefox or configure POSTMAN RestClient in Chrome. download addons for chrome and Mozilla firefox
Now publlish and run your application
http://localhost:8080/YOUR_PROJECTNAME/service/helloworld
for post Method {"greeting":"Hello World"} will be passed in request
Ads