Restlet Webservices Hello World:
Download Code:
https://www.box .com/s/88ttn0jo d0hhr9727e3b
Download Code:
https://www.box
STEP 1: CREATE Java Project with name helloWorld
STEP 2: Create Packages(same name as given below)
1> com.rest.api
2> com.rest.app
3> com.rest.db
4> com.rest.resource
1> com.rest.api
2> com.rest.app
3> com.rest.db
4> com.rest.resource
STEP 3: Creat class helloWorld.java in com.rest.api package
package com.rest.api;
import java.io.Serializable;
public class helloWorld implements Comparable<helloWorld>, Serializable {
private static final long serialVersionUID = 1l;
protected String result = "";
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String toString() {
return result;
}
@Override
public int compareTo(helloWorld o) {
// TODO Auto-generated method stub
return 0;
}
}
package com.rest.api;
import java.io.Serializable;
public class helloWorld implements Comparable<helloWorld>, Serializable {
private static final long serialVersionUID = 1l;
protected String result = "";
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String toString() {
return result;
}
@Override
public int compareTo(helloWorld o) {
// TODO Auto-generated method stub
return 0;
}
}
STEP 4: Create class WebServiceApplicationOpenSER.java in com.rest.app package
package com.rest.app;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.StringRepresentation;
import com.rest.resource.helloWorldResource;
public class WebServiceApplicationOpenSER extends Application {
public static void main(String[] args) throws Exception {
// Create a component
Component component = new Component();
// PVN: Changed default port so we can run this service on the
// same machine as the default web service
component.getServers().add(Protocol.HTTP, 8100);
component.getClients().add(Protocol.FILE);
WebServiceApplicationOpenSER application = new WebServiceApplicationOpenSER();
// Disable the logger for the web service
component.getLogService().setEnabled(false);
System.out.println("hello world start");
component.getDefaultHost().attach(application);
// Attach the application to the component and start it
component.start();
}
@Override
public Restlet createRoot() {
Router router = new Router(getContext());
// The helloWorld resource
router.attach("/hello",helloWorldResource.class);
Restlet mainpage = new Restlet() {
@Override
public void handle(Request request, Response response) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<html>");
stringBuilder
.append("<head><title>Application Login Openser Sample Page</title></head>");
stringBuilder.append("<body bgcolor=white>");
stringBuilder.append("<table border=\"0\">");
stringBuilder.append("<tr>");
stringBuilder.append("<td>");
stringBuilder.append("<h1>Login Openser Web Service - REST</h1>");
stringBuilder.append("</td>");
stringBuilder.append("</tr>");
stringBuilder.append("</table>");
stringBuilder.append("</body>");
stringBuilder.append("</html>");
response.setEntity(new StringRepresentation(stringBuilder
.toString(), MediaType.TEXT_HTML));
}
};
router.attach("", mainpage);
return router;
}
}
package com.rest.app;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.StringRepresentation;
import com.rest.resource.helloWorldResource;
public class WebServiceApplicationOpenSER extends Application {
public static void main(String[] args) throws Exception {
// Create a component
Component component = new Component();
// PVN: Changed default port so we can run this service on the
// same machine as the default web service
component.getServers().add(Protocol.HTTP, 8100);
component.getClients().add(Protocol.FILE);
WebServiceApplicationOpenSER application = new WebServiceApplicationOpenSER();
// Disable the logger for the web service
component.getLogService().setEnabled(false);
System.out.println("hello world start");
component.getDefaultHost().attach(application);
// Attach the application to the component and start it
component.start();
}
@Override
public Restlet createRoot() {
Router router = new Router(getContext());
// The helloWorld resource
router.attach("/hello",helloWorldResource.class);
Restlet mainpage = new Restlet() {
@Override
public void handle(Request request, Response response) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<html>");
stringBuilder
.append("<head><title>Application Login Openser Sample Page</title></head>");
stringBuilder.append("<body bgcolor=white>");
stringBuilder.append("<table border=\"0\">");
stringBuilder.append("<tr>");
stringBuilder.append("<td>");
stringBuilder.append("<h1>Login Openser Web Service - REST</h1>");
stringBuilder.append("</td>");
stringBuilder.append("</tr>");
stringBuilder.append("</table>");
stringBuilder.append("</body>");
stringBuilder.append("</html>");
response.setEntity(new StringRepresentation(stringBuilder
.toString(), MediaType.TEXT_HTML));
}
};
router.attach("", mainpage);
return router;
}
}
STEP 5: Create class helloWorldDAO.java in com.rest.db package
package com.rest.db;
import com.rest.api.helloWorld;
public class helloWorldDAO {
//create all database related code here
//u can create select delete update and insert query also
public helloWorld find(){
helloWorld hw = null;
try{
hw = new helloWorld();
hw.setResult("Hello Darling");
}catch(Exception e){
e.printStackTrace();
}
return hw;
}
}
package com.rest.db;
import com.rest.api.helloWorld;
public class helloWorldDAO {
//create all database related code here
//u can create select delete update and insert query also
public helloWorld find(){
helloWorld hw = null;
try{
hw = new helloWorld();
hw.setResult("Hello Darling");
}catch(Exception e){
e.printStackTrace();
}
return hw;
}
}
STEP 6: Create class helloWorldResource.java in com.rest.resource package
package com.rest.resource;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
import com.rest.api.helloWorld;
import com.rest.db.helloWorldDAO;
public class helloWorldResource extends Resource{
private helloWorld _hw = null;
public helloWorldResource(Context context, Request request, Response response){
super( context, request, response );
String id = null;
id = (String) getRequest().getAttributes().get("id");
_hw = find();
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}
private helloWorld find(){
try{
helloWorldDAO dao = new helloWorldDAO();
return dao.find();
}catch (Exception e ){
return null;
}
}
@Override
public Representation represent(Variant variant) throws ResourceException {
Representation result = null;
if (_hw == null) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
result = new StringRepresentation("Error in Display");
return result;
}else{
result = new StringRepresentation(_hw.toString());
}
return result;
}
}
package com.rest.resource;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;
import com.rest.api.helloWorld;
import com.rest.db.helloWorldDAO;
public class helloWorldResource extends Resource{
private helloWorld _hw = null;
public helloWorldResource(Context context, Request request, Response response){
super( context, request, response );
String id = null;
id = (String) getRequest().getAttributes().get("id");
_hw = find();
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}
private helloWorld find(){
try{
helloWorldDAO dao = new helloWorldDAO();
return dao.find();
}catch (Exception e ){
return null;
}
}
@Override
public Representation represent(Variant variant) throws ResourceException {
Representation result = null;
if (_hw == null) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
result = new StringRepresentation("Error in Display");
return result;
}else{
result = new StringRepresentation(_hw.toString());
}
return result;
}
}
STEP 7: Create jar file
-----> select File -----> Export -----> select Java -----> select Runnable JAR file -----> NEXT -----> select helloWorld project in Launch Configuration ----->
Give Export Destination With File name helloWorld.jar-----> Select Library Handling: Package Required libraries into generated JAR -----> Finish
-----> select File -----> Export -----> select Java -----> select Runnable JAR file -----> NEXT -----> select helloWorld project in Launch Configuration ----->
Give Export Destination With File name helloWorld.jar-----> Select Library Handling: Package Required libraries into generated JAR -----> Finish
STEP 8: Extract jar file (in current folder)
STEP 9: EXPORT CLASSPATH (as par below image)
NOTE: In place of /root/Desktop/ give your jar file path
CLASSPATH=$JAVA_HOME/lib/rt.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/com.noelios.restlet.ext.servlet_2.5.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/com.noelios.restlet.ext.simple_3.1.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/com.noelios.restlet.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.restlet.ext.fileupload_1.2.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.restlet.ext.json_2.0.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.restlet.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.simpleframework.jar
java -classpath $CLASSPATH com.rest.app.WebServiceApplicationOpenSER
Expected output: hello world start
NOTE: In place of /root/Desktop/ give your jar file path
CLASSPATH=$JAVA_HOME/lib/rt.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/com.noelios.restlet.ext.servlet_2.5.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/com.noelios.restlet.ext.simple_3.1.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/com.noelios.restlet.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.restlet.ext.fileupload_1.2.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.restlet.ext.json_2.0.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.restlet.jar
CLASSPATH=$CLASSPATH:/root/Desktop/helloWorld/org.simpleframework.jar
java -classpath $CLASSPATH com.rest.app.WebServiceApplicationOpenSER
Expected output: hello world start
STEP 10: HOW TO RUN(type this command in your command prompt)
GET curl -X GET http://localhost:8100/hello
Expected output: Hello Darling
GET curl -X GET http://localhost:8100/hello
Expected output: Hello Darling
No comments:
Post a Comment