RESTFul API設計指南及使用說明

RESTFul API設計指南及使用說明

1、 協議
API與用戶的通訊協議,使用HTTP協議html


2、 域名
應儘可能將API部署在專用域名之下http://api.example.com
也能夠將API放在主域名下http://example.com/api

3、 版本
應該將API的版本號放入URLhttp://example.com/api/v1.0

4、 路徑與映射
REST的關鍵原則與將你的API分割成邏輯資源緊密相關。使用HTTP請求控制這些資源,sql

RESTFul原則提供了HTTP methods映射做爲策略來處理CRUD actions,api


須要繼承Resource父類以下:

@API():類註解,括號類爲類請求路徑,如@API("/api/v1.0")

@GET():方法註解,查詢,括號內爲方法請求路徑,如GET("/user"),若是經過API直接訪問,括號可不加
@GET("/:id"):方法註解,根據ID查詢,括號內表示可根據ID直接查詢結果
@POST():方法註解,新增,括號內爲方法請求路徑,若是經過API直接訪問,括號可不加
@PUT():方法註解,修改,括號內爲方法請求路徑,若是經過API直接訪問,括號可不加
@DELETE():方法註解,刪除,括號內爲方法請求路徑,若是經過API直接訪問,括號可不加


5、 資源(Model)經常使用方法,須要繼承Model父類
1:findBy(String where,Object... params):根據條件查詢spa

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):根據條件查詢,返回第一條數據code

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():查詢全部htm

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):查詢相關列信息blog

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()獲取總條數

相關文章
相關標籤/搜索