June 13, 2012 at 10:42 AM
**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
- org.json.jar
org.restlet.ext.json-2.1-SNAPSHOT.jar
org.restlet.ext.servlet-2.1-SNAPSHOT.jar
org.restlet.ext.xml-2.1-SNAPSHOT.jar
org.restlet-2.1-SNAPSHOT.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
json_simple-1.1.jar log4j-1.2.16.jar
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 we have to pass the applicationClass generated for our application.
I have created HelloWorldApplication.java.
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
Related Tutorials/Questions & Answers:
Restlet
Restlet
Restlet is a lightweight REST framework for
Java it's
features is REST concepts have equivalent
Java classes (resource,
representation, connector, etc.)
Read full
Restlet Frame work Related questionRestlet Frame work Related question How to send data from client to server using
Restlet framework. The main problem is i am not able to get data at server side. Can anyone post a complete program how to achieve
Advertisements
Example of HashSet class in javaExample of HashSet class in
java.
In this part of tutorial, we... unique. You can not store duplicate value.
Java hashset
example.
How....
Example of Hashset iterator method in
java.
Example of Hashset size() method
What is array in java with example?What is array in
java with
example? Hi,
I am beginner in
Java and want to learn
Java Array concepts. Which is the best tutorials for learn
Java Array with
example codes?
What is array in
java with
example?
Thanks
Java FTP Client ExampleJava FTP Client Example How to write
Java FTP Client
Example code?
Thanks
Hi,
Here is the
example code of simple FTP client in
Java which downloads image from server FTP Download file
example.
Thanks
Example of HashMap class in javaExample of HashMap class in
java.
The HashMap is a class in
java collection framwork. It stores values in the
form of key/value pair. It is not synchronized
FTP Java exampleFTP
Java example Where is the FTP
Java example on your website? I am... examples of FTP at: FTP Programming in
Java tutorials with
example code.
Thaks... functionality in my
Java based application.
My application is swing based and I have
freemarker example - Java Beginners an
example for freemarker. i want to get the values from
java and display those values in the page designed using freemarker(how to get the values from
java). and please provide an
example with code and directory structure. send me ASAP
Example Code - Java BeginnersExample Code I want simple Scanner Class
Example in
Java and WrapperClass
Example.
What is the Purpose of Wrapper Class and Scanner Class .
when i compile the Scanner Class
Example the error occur :
Can not Resolve symbol
Java FTP ExampleJava FTP Example Is there any
java ftp
example and tutorials... and tutorials that teaches you how to user FTP in your
Java project.
Most commonly used FTP api in
java is Apache FTP.
Browse all the FTP tutorials at
Java FTP
java string comparison examplejava string comparison example how to use equals method in String... strings are not same.
Description:-Here is an
example of comparing two strings using equals() method. In the above
example, we have declared two string
Java nested class exampleJava nested class example Give me any
example of Nested Class.
Nested Class: Class defined within another class is called nested class... class.
Example:
public class NestedClass{
private String outer = "Outer
throws example program javathrows
example program java how to use throws exception in
java?
The throws keyword is used to indicate that the method raises..." java.lang.ArithmeticException: / by zero
Description:- Here is an
example of throws clause. We
exampleexample
example on Struts framework
exampleexample
example on Struts framework
Java Comparable ExampleJava Comparable Example I want to know the use of Comparable Interface. Please provide me one
example
Comparable interface is used....
Here is an
example that compares two ages using Comparable Interface.
import
Java hashset example.Java hashset
example.
HashSet is a collection. You can not store duplicate value in
HashSet.
In this
java hashset exmple, you will see how to create HashSet in
java
application and how to store value in Hashset
Java collection Stack example :- -1
Description:- The above
example demonstrates you the Stack class in
java...
Java collection Stack example How to use Stack class in
java.... Here is an
example of Stack class.
import java.util.Stack;
public class
Inheritance java ExampleInheritance
java Example How can we use inheritance in
java program... for bread
Description:- The above
example demonstrates you the concept... properties of the superclass. In the given
example, the class Animal is a superclass
Java Comparator ExampleJava Comparator Example Can you provide me an
example of Comparator Interface?
A comparator object is capable of comparing two different... is an
example that compares the object of class Person by age.
import java.util.
Java HashMap example.Java HashMap
example.
The HashMap is a class in
java. It stores values in name values pair. You can
store null value of key and values.
Here... of map.
Code:
HashMapExample .
java
(adsbygoogle
Java Map ExampleJava Map Example How we can use Map in
java collection?
The Map interface maps unique keys to value means it associate value to unique...
Description:- The above
example demonstrates you the Map interface. Since Map
Java File Management Example of file.
Read the
example Read file in
Java for more information on reading...
Java File Management Example Hi,
Is there any ready made API in
Java for creating and updating data into text file? How a programmer can write code
exampleexample i need ex on struts-hibernate-spring intergration
example
Struts Spring Hibernate Integration
printing example - Java Beginnersprinting example Is it possible to print
java controls using print method?
My problem is to print a student mark list using
java?
The mark list should like that of university mark list
Java collection Queue ExampleJava collection Queue Example How can we use Queue in
java... LinkedList();
queue.add("
Java");
queue.add("DotNet...) {
new MainDemo().queueExample();
}
}
Output:-
remove:
Java
element
Java Queue ExampleJava Queue Example how to use queue in
java ?
import...();
queue.add("
Java");
queue.add("DotNet");
queue.offer("PHP...().queueExample();
}
}
Output:-
remove:
Java
element: DotNet
poll: DotNet
peek
Synchronized with example - Java BeginnersSynchronized with example Hi Friends, I am beginner in
java. what i know about synchonized keyword is,If more that one 1 thread tries to access a particular resource we can lock the method using synchronized keyword. Then after
Java Example projects about STRUTSJava Example projects about STRUTS Hai...
I completed MCA but i have no job in my hands.
But i do some small projects about STRUTS.
Please send me some
example projects about STRUTS.
Please visit the following link
Java ArrayList ExampleJava ArrayList Example How can we use array list in
java program ?
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String [] args){
ArrayList<String> array = new
Java FTP file upload example Programming in
Java tutorials with
example code.
Thanks...
Java FTP file upload example Where I can find
Java FTP file upload
example? What is the link of the FTP examples on your website?
Thanks
 
Java Example Codes and Tutorials in 2017Java Example Codes and Tutorials in 2017 Hi,
Which is the best tutorials to learn
Java programming language in 2014?
Thanks
HI,
Check tutorial at
Java tutorials.
All
Java tutorials at
Java Tutorials and examples
Inheritance Example In JavaInheritance
Example In
Java
In this section we will read about the Inheritance using a simple
example.
Inheritance is an OOPs feature that allows to inherit... talk about the Inheritance in
Java the Multiple inheritance is
not supported
Example of contains method of hashset in java.
Example of contains method of hashset in
java.
The contains() is a method of hashset. It is used for checking that the
given number is available..., it returns true otherwise false.
Code:
HashSetToArray .
java Example of Hashset iterator method in java.Example of Hashset iterator method in
java.
In this exmple of HashSet class, you will see the use of iterator()
method. It is used for traversing all element from HashSet.
Code:
HashSetRemoveElement.java
package