RESTFul API设计指南及使用说明

RESTFul API设计指南及使用说明

一、 协议
API与用户的通信协议,使用HTTP协议


二、 域名
应尽量将API部署在专用域名之下http://api.example.com
也可以将API放在主域名下http://example.com/api

三、 版本
应该将API的版本号放入URLhttp://example.com/api/v1.0

四、 路径与映射
REST的关键原则与将你的API分割成逻辑资源紧密相关。使用HTTP请求控制这些资源,

RESTFul原则提供了HTTP methods映射作为策略来处理CRUD actions,


需要继承Resource父类如下:

@API():类注解,括号类为类请求路径,如@API("/api/v1.0")

@GET():方法注解,查询,括号内为方法请求路径,如GET("/user"),如果通过API直接访问,括号可不加
@GET("/:id"):方法注解,根据ID查询,括号内表示可根据ID直接查询结果
@POST():方法注解,新增,括号内为方法请求路径,如果通过API直接访问,括号可不加
@PUT():方法注解,修改,括号内为方法请求路径,如果通过API直接访问,括号可不加
@DELETE():方法注解,删除,括号内为方法请求路径,如果通过API直接访问,括号可不加


五、 资源(Model)常用方法,需要继承Model父类
1:findBy(String where,Object... params):根据条件查询

1 public List<M> findBy(String username,String password){
2 Model modelDao = new Model();
3 List<M> list = modelDao.findBy("username=? AND password=?",username,password);
4 return list;
5 }


2:findById(Object id):根据ID查询

1 public Model findById(Object id){
2 Model modelDao = new Model();
3 Model model = modelDao.findById(id);
4 return model;
5 }


3: findFirstBy(String where,Object... params):根据条件查询,返回第一条数据

1 public Model findFirstBy(String username,String password){
2 Model modelDao = new Model();
3 Model model = modelDao.findFirstBy("username=? AND password=?",username,password);
4 return model;
5 }


4: findAll():查询所有

1 public List<M> findAll(){
2 Model modelDao = new Model();
3 List<M> list = modelDao.findAll();
4 return list;
5 }


5:findColsBy(String colums, String where, Object... params):查询相关列信息

1 public List<M> findColsBy(String username,String password){
2 Model modelDao = new Model();
3 List<M> list = modelDao.findColsBy("id,username,password","username=? AND password=?",username,password)
4 return list;
5 }


6: save():添加

1 public boolean save(){
2 Model model = new Model();
3 model.set("username","test");
4 model.set("password","123456");
5 boolean flag = model.save();
6 return flag;
7 }


7:save(Model model):添加

1 public boolean save(Model model){
2 Model modelDao = new Model();
3 boolean flag = modelDao.save(model);
4 return flag;
5 }

 

8:save(List<M> list):批量添加

1 public boolean save(List<M> list){
2 Model modelDao = new Model();
3 boolean flag = modelDao.save(list);
4 return flag;
5 }


9:update():更新

1 public boolean update(){
2 Model model = new Model();
3 model.set("id",1);
4 model.set("username","test");
5 model.set("password","123456");
6 boolean flag = model.update();
7 return flag;
8 }


10:update(String sql,Object... params):更新或删除 sql为更新语句时更新,为删除语句时删除

1 public boolean update(String username,String password,int id){
2 Model modelDao = new Model();
3 String sql = "update user set username=?,password=? where id=?";
4 boolean flag = modelDao.update(sql,username,password,id);
5 return flag;
6 }


11:delete():删除

1 public boolean delete(){
2 Model model = new Model();
3 model.set("id",1);
4 boolean flag = model.delete();
5 return flag;
6 }


12:deleteBy(String where,Object... params):根据条件删除

1 public boolean deleteBy(String username,String password){
2 Model modelDao = new Model();
3 boolean flag = modelDao.deleteBy("username=? AND password=?",username,password);
4 return flag;
5 }


13:deleteById(Object id):根据ID删除

1 public boolean deleteById(int id){
2 Model modelDao = new Model();
3 boolean flag = modelDao.deleteById(id);
4 return flag;
5 }


14:fullPaginate(int pageNumber, int pageSize, String sql, Object... params):分页查询

1 public FullPage<M> fullPaginate(int pageNumber, int pageSize,String username,String password) {
2 Model modelDao = new Model();
3 String sql = "select * from user where username=? AND password=?";
4 FullPage<M> fullPage = modelDao.fullPage(pageNumber,pageSize,sql,username,password);
5 return fullPage;
6 }

//注:fullPage.getList()获取查询结果 fullPage.getTotalRow()获取总条数

posted @ 2017-03-15 09:52  小小渔夫  阅读(1127)  评论(0编辑  收藏  举报
友情链接: 燕归来兮 李狗蛋Blog 萌力觉醒