RESTFul API Design Guidelines and Instructions for Use

RESTFul API Design Guidelines and Instructions for Use

1. Protocol
The communication protocol between the API and the user uses the HTTP protocol .


2. Domain name
The API should be deployed under the dedicated domain name ( http://api.example.com )
or the API under the main domain name ( http://example.com/api )

Third, the version
should be the API Put the version number into the URL ( http://example.com/api/v1.0 )

4. Paths and Mapping
The key principle of REST is closely related to dividing your API into logical resources. control these resources using HTTP requests,

The RESTFul principle provides a map of HTTP methods as a strategy for handling CRUD actions,


The parent class of Resource needs to be inherited as follows:

@API(): class annotation, the bracketed class is the class request path, such as @API("/api/v1.0")

@GET(): method annotation, query, method request in brackets The path, such as GET("/user"), if accessed directly through the API, the parentheses can be
omitted @GET("/:id"): method annotation, query according to ID, the brackets indicate that the result can be directly queried according to ID
@POST() : Method annotation, new, the method request path in parentheses, if accessed directly through the API, the parentheses can be without
@PUT(): Method annotation, modified, the method request path in parentheses , if accessed directly through the API, the brackets can be without
@PUT () DELETE (): Method annotation, delete, the method request path is in the brackets, if it is accessed directly through the API, the brackets can be omitted. params): query based on conditions



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): Query according to conditions and return the first data

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 sql is update when the update statement, delete when the delete 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 according to 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 according to 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569731&siteId=291194637