RESTFul API设计指南及使用说明

时间:2023-05-21 12:31:20

RESTFul API设计指南及使用说明

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

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

三、 版本
应该将API的版本号放入URL(http://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):根据条件查询

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

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

 public Model findById(Object id){
Model modelDao = new Model();
Model model = modelDao.findById(id);
return model;
}

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

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

4: findAll():查询所有

 public List<M> findAll(){
Model modelDao = new Model();
List<M> list = modelDao.findAll();
return list;
}

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

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

6: save():添加

 public boolean save(){
Model model = new Model();
model.set("username","test");
model.set("password","123456");
boolean flag = model.save();
return flag;
}

7:save(Model model):添加

 public boolean save(Model model){
Model modelDao = new Model();
boolean flag = modelDao.save(model);
return flag;
}

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

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

9:update():更新

 public boolean update(){
Model model = new Model();
model.set("id",1);
model.set("username","test");
model.set("password","123456");
boolean flag = model.update();
return flag;
}

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

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

11:delete():删除

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

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

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

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

 public boolean deleteById(int id){
Model modelDao = new Model();
boolean flag = modelDao.deleteById(id);
return flag;
}

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

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

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