restlet-jse

2024-03-31 14:38
文章标签 jse restlet

本文主要是介绍restlet-jse,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、实体

Contact 

 

 

package com.tht.common;import java.io.Serializable;public class Contact implements Serializable {private int id;private static final long serialVersionUID = 1L;private int age;private String firstName;private Address homeAddress;private String lastName;public Contact() {}public Contact(String firstName, String lastName, Address homeAddress,int age) {super();this.firstName = firstName;this.lastName = lastName;this.homeAddress = homeAddress;this.age = age;}public Contact(int id,String firstName, String lastName, Address homeAddress,int age) {super();this.id=id;this.firstName = firstName;this.lastName = lastName;this.homeAddress = homeAddress;this.age = age;}public int getAge() {return age;}public String getFirstName() {return firstName;}public Address getHomeAddress() {return homeAddress;}public String getLastName() {return lastName;}public void setAge(int age) {this.age = age;}public void setFirstName(String firstName) {this.firstName = firstName;}public void setHomeAddress(Address homeAddress) {this.homeAddress = homeAddress;}public void setLastName(String lastName) {this.lastName = lastName;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic boolean equals(Object obj) {return super.equals(obj);}}

 

 

二、实体  Address

 

package com.tht.common;import java.io.Serializable;public class Address implements Serializable {private static final long serialVersionUID = 1L;private String line1;private String line2;private String zipCode;private String city;private String country;public Address() {}public Address(String line1, String line2, String zipCode, String city,String country) {super();this.line1 = line1;this.line2 = line2;this.zipCode = zipCode;this.city = city;this.country = country;}public String getCity() {return city;}public String getCountry() {return country;}public String getLine1() {return line1;}public String getLine2() {return line2;}public String getZipCode() {return zipCode;}public void setCity(String city) {this.city = city;}public void setCountry(String country) {this.country = country;}public void setLine1(String line1) {this.line1 = line1;}public void setLine2(String line2) {this.line2 = line2;}public void setZipCode(String zipCode) {this.zipCode = zipCode;}}
 

三、服务资源

 

package firstSteps;import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;import com.tht.resource.ContactServerResource;public class FirstStepsApplication extends Application {/*** Creates a root Restlet that will receive all incoming calls.*/@Overridepublic synchronized Restlet createInboundRoot() {// Create a router Restlet that routes each call to a new instance of HelloWorldResource.Router router = new Router(getContext());// Defines only one routerouter.attach("/hello", ContactServerResource.class);return router;}/* public static void main(String[] args) throws Exception {  // Create a new Component.  Component component = new Component();  // Add a new HTTP server listening on port 8182.  component.getServers().add(Protocol.HTTP, 8182);  // Attach the sample application.  component.getDefaultHost().attach("/firstSteps",  new FirstStepsApplication());  // Start the component.  component.start();  }          */
}
 

 

package com.tht.resource;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.restlet.resource.ServerResource;import com.tht.common.Address;
import com.tht.common.Contact;
import com.tht.common.ContactResource;public class ContactServerResource extends ServerResource implementsContactResource {public ContactServerResource(){}private static    Contact contact = new Contact("firstName1", "lastName1", new Address("Address1", "Address2", "20010", "city1", "country1"), 10);@Overridepublic void remove(String key) {contact=null;}@Overridepublic  Contact retrieve() {return contact;}@Overridepublic void store(Contact contact) {this.contact=contact;}}
 

 

 

三启动服务

 

import org.restlet.Component;
import org.restlet.data.Protocol;import firstSteps.FirstStepsApplication;/*** 启动程序 开起服务http://localhost:8182/firstSteps* *可以访问请求路径: http://localhost:8182/firstSteps/hello* @author think**/
public class Run {public static void main(String[] args) throws Exception {  // Create a new Component.  Component component = new Component();  // Add a new HTTP server listening on port 8182.  component.getServers().add(Protocol.HTTP, 8182);  // Attach the sample application.  component.getDefaultHost().attach("/firstSteps",  new FirstStepsApplication());  // Start the component.  component.start();  }          }
 

 

四、启动客户端

package com.tht.client;import com.tht.common.Contact;
import com.tht.common.ContactResource;
import java.io.IOException;
import java.util.List;
import java.util.Map;import org.restlet.data.MediaType;import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;public class GetRequest {public static void main(String[] args) throws ResourceException,IOException {ClientResource cr = new ClientResource("http://localhost:8182/firstSteps/hello");// Get the Contact objectContactResource resource = cr.wrap(ContactResource.class);Contact contact = resource.retrieve();if (contact != null) {System.out.println("firstname: " + contact.getFirstName());System.out.println(" lastname: " + contact.getLastName());System.out.println("      age: " + contact.getAge());}/** Map<String, Contact> contacts = resource.retrieve(); Contact* contact=null; for(Object key : contacts.keySet()){* System.out.println("key:"+key);* * contact=contacts.get(key); System.out.println("value:"+contact); if* (contact != null) { System.out.println("firstname: " +* contact.getFirstName()); System.out.println(" lastname: " +* contact.getLastName()); System.out.println("      age: " +* contact.getAge()); }* * }*/// In case the Contact object is not available// Get a JSON representation of the contact// System.out.println("\nJSON representation");// cr.get(MediaType.APPLICATION_JSON).write(System.out);}
}
 

delete

 

package com.tht.client;
import com.tht.common.Address;
import com.tht.common.Contact;
import com.tht.common.ContactResource;
import java.io.IOException;import org.restlet.data.MediaType;import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;public class DeleteRequest {public static void main(String[] args) throws ResourceException,IOException {ClientResource cr = new ClientResource("http://localhost:8182/firstSteps/hello");// Get the Contact objectContactResource resource = cr.wrap(ContactResource.class);Contact contact = new Contact("firstName1", "lastName1", new Address("Address1", "Address2", "20010", "city1", "country1"), 10);resource.store(contact);}
}
 

 

put

 

package com.tht.client;
import com.tht.common.Address;
import com.tht.common.Contact;
import com.tht.common.ContactResource;
import java.io.IOException;import org.restlet.data.MediaType;import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;public class PutRequest {public static void main(String[] args) throws ResourceException,IOException {ClientResource cr = new ClientResource("http://localhost:8182/firstSteps/hello");// Get the Contact objectContactResource resource = cr.wrap(ContactResource.class);Contact contact = new Contact("firstName1", "lastName1", new Address("Address1", "Address2", "20010", "city1", "country1"), 10);resource.store(contact);}
}
 

 

 

这篇关于restlet-jse的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/864578

相关文章

Restlet 学习笔记 轻量级 restfull框架

转 RESTLET开发实例(三)基于spring的REST服务 发表于2年前(2013-07-23 15:49)   阅读( 1943) | 评论( 1)  16人收藏此文章, 我要收藏 赞 0 1月16日厦门 OSC 源创会火热报名中,奖品多多哦   前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务。这里将介绍rest

Restlet+Fastjson 快速构建轻量级 Java RESTful Webservice

自己入门Java时做过一个小型RESTful Web Service的项目,这里总结一下。服务的数据交换格式主要采用JSON,服务为REST风格,连接采用Http协议,数据库使用MySQL,OR Mapping采用的是Hibernate.  小数据直接用URL传参,配合Restlet 的强大的 URI重写重定向,层级URI路由功能,更是十分的方便。数据大时就是用JSON,配合强大的Fastjson

Restlet edition for Java EE FirstStepsServlet

使有方法   一、该项目部署到Tomcat服务器下,启动Tomcat服务器 二、在浏览器地址栏中输入:http://localhost:8080/firstStepsServlet/restlet/hello 输出:成功       方法二:   一、不要开启服务器,直接启动类Run 二、在浏览器地址栏中输入:http://localhost:8182/firstSte

restlet中stable, testing 和 unstable releases之间的区别

Restlet Framework包含三个不同的发布类型: 包括   稳定版(stable),测试版(testing) 和 不稳定版(unstable)是参照 Debian Linux 的发行版的规则。 "Stable" is the release that we recommend for applications in production. The API of this re

Restlet restful 学习

首先添加 Restlet 需要的Maven Dependency:   <dependency><groupId>org.restlet.jse</groupId><artifactId>org.restlet</artifactId><version>${restlet-version}</version></dependency><dependency><groupId>org.

camel发布restlet

<?xml version="1.0" encoding="UTF-8"?><blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:cm="http://aries.apache.org/blueprint/xml

JSE学习笔记---关于数据类型那点事儿

本文总结了三个不容易被注意到的三个数据类型的特殊用法,       1.  二进制         java7以后,可以用前缀”0b“表示二进制数,也可以在数字之间,为数字加下划线分割,方便阅读。         例如:             0b1110_1010_0101=0b111010100101       2. 浮点数             浮点数不适用于金融等精确计算

一例jse蠕虫的分析

概述 这是一例jse格式的蠕虫病毒,会隐藏系统中所有的doc、docx和rtf文件,创建同名的.jse文件,诱导用户点击执行,通过感染U盘和网络驱动器、光盘刻录临时文件夹、html文件进行传播。 这个样本是使用JScript语言编写的加密脚本文件,可以使用cscript.exe来执行,分析起来没有难度,但是其修改注册表的操作的还是很高明的。 样本信息 MD5: 4d1a9f7559de52