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相关推荐
- Restlet restful 学习
首先添加 Restlet 需要的Maven Dependency: <dependency><groupId>org.restlet.jse</groupId>&l ...
- nutch2.1在windows平台上使用eclipsedebug 存储在mysql的搭建过程
步骤1:准备好eclipse.eclipse svn插件.mysql准备好,mysql使用utf-8编码 步骤2:mysql建库,建表: CREATE DATABASE nutch ; ...
- 20150420-20150424 一周工作问题及解决【共享文件的获取、前后台乱码问题解决等】
20150420-20150424问题记录 1.MD5加密 原文经过MD5加密后,得到唯一的摘要. 一个摘要可对应多条原文.故:根据摘要不能逆推出原文. 2.关于InputStream.availab ...
- RESTLET开发实例(二)使用Component、Application的REST服务
2019独角兽企业重金招聘Python工程师标准>>> 上一篇文章,我们介绍了基于JAX-RS的REST服务,本篇文章我们介绍不基于JAX-RS的模式.JAX-RS其实就是一个简单的 ...
- Restlet入门示例
http://cunfu.iteye.com/blog/757467 本文的目的在于完成一个Restlet入门示例. 首先,是一个Stand alone应用. 然后,将其部署与Tomcat容器中. 最 ...
- 用Restlet创建面向资源的服务
http://www.infoq.com/cn/articles/restlet-for-restful-service Restlet项目(http://www.restlet.org)为" ...
- RESTLET开发实例(三)基于spring的REST服务
http://www.lifeba.org/arch/restlet_spring_3.html 前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将 ...
- 第10步 (1)logback.xml日志配置(2) ftp(上传文件)服务器配置(3) idea注入和自动编译配置(4)项目提交gitee(5)fe助手和restlet client
**************************************************************************************************** ...
- Restlet框架– Hello World示例
Restlet是用于Java平台的轻量级,全面的开源REST框架. Restlet适用于服务器和客户端Web应用程序. 它支持主要的Internet传输,数据格式和服务描述标准,例如HTTP和HTTP ...
- [转贴]JAVA:RESTLET开发实例(三)基于spring的REST服务
前面两篇文章,我们介绍了基于JAX-RS的REST服务以及Application的Rest服务.这里将介绍restlet如何整合spring框架进行开发.Spring 是一个开源框架,是为了解决企业应 ...
最新文章
- 【数据结构与算法】之深入解析“键盘行”的求解思路与算法示例
- android内容提供者_挖穿Android第三十九天
- 为什么eval某个json字符串时要加括号?
- input 函数的输入和输出
- Ajax学习笔记-基础概述-1
- oracle全库导入 imp,imp导入全数据库
- JMeter(三):后置处理器[Regular Expression Extractor]
- 网站接口被恶意攻击怎么办
- java加减乘除判断代码_JAVA实现精确的加减乘除代码
- 数字翻译成英语JavaScript
- Matlab求正态函数积分,积分对应的分位点
- 这个七夕,送你一份程序员教科书级别的告白指南
- nodejs 遍历目录(文件夹)下的所有文件
- 【组合数学】递推方程 ( 特解形式 | 特解求法 | 特解示例 )
- 损失函数——KL散度(Kullback-Leibler Divergence,KL Divergence)
- debounce 防抖函数
- JAVAWEB——监听器(Listener),监听器编写步骤,八大监听器的介绍
- mysql中的coalesce用法
- 【C++】【数据结构】顺序栈的基本操作(初始化、入栈、出栈、取栈顶元素、遍历输出栈)的算法实现附全代码
- 贪心算法解决活动安排-Python实现(排序+贪心选择)
热门文章
- tomcat的卸载与安装
- MATLAB轻松绘制地图路线——Dijkstra(迪杰斯特拉)算法最短路径规划
- 教练式管理工具与技术(附58页课件下载)
- 【大数据】六、频繁项集与关联规则(频繁项集、关联规则、A-Priori、PCY)
- 索引的优缺点以及如何创建索引
- C语言生成随机数代码
- 小猫爪:PMSM之FOC控制08-状态观测器的引入
- pythonic 代码_如何编写Pythonic的代码?
- linux就该这么学第8章Iptables与Firewalld防火墙。
- stm32毕设 stm32的智能婴儿车系统(源码+硬件+论文)