Restlet开发指南.doc_第1页
Restlet开发指南.doc_第2页
Restlet开发指南.doc_第3页
Restlet开发指南.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Restlet开发指南1. Java对象Java对象是实际操作对象的封装,目前可以从http提交的xml格式和Json格式自动转换为Java类对象。1.1. XML转换XmlRootElement(name=”user”)public class User private String name; private String sex; private int id; public void setName(String name) = name; public void setId(int id)this.id = id; public void setSex(String sex)this.sex = sex; public String getName()return ; public String getSex()return this.sex; public int getId() return this.id; 如果http客户端提交的数据格式是xml, Json male 10001Restlet将会自动转化成一个User对象。2. Resource对象Path(“/users”)class UsersResource GET Produces(“application/xml”,”text/xml”) public UserList getUsers() . /UserList也必须是个可转换xml的对象 GET Path(“/id:d+”) Produces(“application/xml”,”text/xml”) public User getUser(PathParam(“id”) int id) . POST Consumes(“application/xml”,”text/xml”) public int addUser(User user) . POST Consumes(“application/json”) public int addUser(Representation rep) JsonRepresentation jrep = new JsonRepresentation(rep); JSONObject jobj = jrep.getJSONObject(); User user = new User(); user.setName(jobj.getString(“name”); user.setSex(jobj.getString(“sex”); user.setId(jobj.getInteger(“id”); return user.getId(); PUT Path(“/id:d+”) Consumes(“application/xml”, “text/xml”) public int updateUser(PathParam(“id”) int id,User usr) . DELETE Path(“/id:d+”) public int deleteUser(PathParam(“id”) int id) . 3. Applicationpublic class TestApplication extends javax.ws.rs.core.Application public TestApplication() super(); public SetClass getClasses() final SetClass classes = new HashSetClass(); classes.add(UsersResource.class); return classes; public Set getSingletons() /final Set singleton = new HashSet(); / singleton.add(new UserResource(); / return singleton; return null; 4. 内部存储在系统运行过程中,返回存储的单一实例public class UserManager private UserManager singleton = new UserManager(); public static UserManager get() return singleton; private Map userDB= new HashMap(); private IntCounter id = new IntCounter(100); public int addUser(User user) this.userDB.put(id.incrementInt(), user); return id.get(); public int updateUser(int id, User user)this.userDB.replace(id, user);return id; public int deleteUser(int id)this.userDB.remove(id);return id; public User getUser(int id)return this.userDB.get(id); 5. 启动RESTpublic class RestMain public static void main(String args) Component com = new Component();Server server = com.getServers(); server.add(Protocol.HTTP,8184);JaxRsApplication app = new JaxRsApplication(com.getContext().createChildContext();app.add(new TestApplication();com.getDefaultHost().attach(app);com.start();System.out.println(Server started on port + server.getPort(); System.out.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论