RESTFul API design guide and instructions for use
RESTFul API design guide and instructions for use
I. Agreement
The communication protocol between APIs and users. HTTP is used.
2. Domain Name
Try to deploy the API under a dedicated domain name (http://api.example.com)
You can also put the API under the primary domain name (http://example.com/api)
Iii. Version
Put the API version number in the URL (http://example.com/api/v1.0)
Iv. Path and ing
The key principles of REST are closely related to splitting your API into logical resources. Use HTTP requests to control these resources,
The RESTFul principle provides HTTP methods ing as a policy to handle CRUD actions,
The Resource parent class must be inherited as follows:
@ API (): Class annotation. The bracket class is the class Request Path, for example, @ API ("/api/v1.0 ")
@ GET (): Method annotation and query. The Request Path is in brackets, for example, GET ("/user"). If you access the request directly through APIS, brackets are not added.
@ GET ("/: id"): Method annotation, Which is queried by ID. The result can be directly queried by ID in brackets.
@ POST (): Method annotation, added. The Method Request Path is enclosed in brackets. If you access the method directly through APIS, brackets are not added.
@ PUT (): Method annotation and modification. The Method Request Path is in brackets. If you access the method directly through APIS, brackets are not added.
@ DELETE (): Method annotation and deletion. The Method Request Path is in brackets. If you access the method directly through APIS, brackets are not added.
5. Common resource (Model) methods, which must inherit the Model parent class
1: findBy (String where, Object... params): query by condition
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): query by 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): returns the first data entry based on the condition query.
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 (): Query all
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): Query related column information
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 (): Add
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): Add
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): Batch add
1 public boolean save(List<M> list){2 Model modelDao = new Model();3 boolean flag = modelDao.save(list);4 return flag;5 }
9: update (): 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): update or delete an SQL statement.
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 (): 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): delete based on conditions
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): delete by 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): Paging Query
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 }
// Note: fullPage. getList () gets the query result fullPage. getTotalRow () gets the total number of entries.