Code quality improvement-Rest style

Preface

Restful is a web software architecture style. It is neither a standard nor a protocol. Although it cannot improve the efficiency of code execution, it can make your code more concise, easy to understand and easy to maintain.

1. What is RESTful?

Restful is a web software architecture style. It is neither a standard nor a protocol. It is used to guide the server on how to design a set of APIs (remote methods) for accessing specific resource APIs. It advocates a unified style of resource positioning and resource operation. (This is a bit similar to the rule of going up and down the stairs to the right in life. It does not need to be strictly enforced like traffic laws. It is more similar to a soft rule. It does not force you to enforce it. You can choose not to comply, then everything will become messy. , You can also choose to follow the rules and make everything organized),

Second, the advantages of Restful

Restful has many advantages. It has a clear structure, conforms to standards, easy to understand, easy to maintain, and easy to access. Therefore, it is being adopted by more and more websites and has become one of the current mainstream styles.

Three, Restful explanation

Very nonsense, don't talk about the cowhide, don't talk about it, get dry!

Before using restful style, request: get, post

CRUD (addition, deletion, modification and check):

http://localhost:8080/addorder?name=1 This is a query request.

http://localhost:8080/deleteorder?name=1 This is a delete request.

http://localhost:8080/updateorder?name=1&pwd=123&uid=21 This is an update request.

http://localhost:8080/insertorder?name=1 This is an add request.

Use restful style, request: post, delete, put, get

CRUD (addition, deletion, modification and check):

http://localhost:8080/order?name=1 This can be any kind of request.

post ---------------- lnsert --------------------- add
delete -------- ----- delete --------------------- delete
put ----------------- update --- ----------------- Modify
get ----------------- select ------------- -------- Inquire

Suppose there is such a get request:
http://localhost:8080/order
Insert picture description here
back-end interface:


/**
 * @Author Hai
 * @Since Created in 2021/2/17
 */

@Controller
@RequestMapping("/order")
public class OrderController {
    
    

    //查询
    @GetMapping("")
    public void getOrder(){
    
    
    //调用的get请求,那我就进到这里来
    //代码块... ... ...
    }

}

If it's not obvious enough, then another post request:
http://localhost:8080/order
Insert picture description here
Comparison of two different requests, but the request method has changed, and the request path is the same.

Let's take a look at the back-end interface:


/**
 * @Author Hai
 * @Since Created in 2021/2/17
 */

@Controller
@RequestMapping("/order")
public class OrderController {
    
    

    //查询
    @GetMapping("")
    public void getOrder(){
    
    
    //如果是get请求,那我就进到这里来
    //代码块... ... ...
    }
   
    //新增
    @PostMapping("")
    public void PostOrder(){
    
    
    //如果是Post请求,那我就进到这里来
    //代码块... ... ...
    }

}

Can you see the effect? ​​In fact, the biggest effect lies in the user experience. No matter you perform any operation of adding, deleting, modifying, and checking, its request address is the same (http://localhost:8080/order), compared to traditional additions, deletions, and modifications. It is undoubtedly much more concise to check that they correspond to four different request addresses. We only need to distinguish different requests corresponding to different request methods.

Use the traditional method: If you want to perform a CRLD operation on an object, the User's server must write four URL addresses. Not conducive to maintenance

Use restful style: The server only needs to publish a user address, and determine the CRUD operation through different request methods. Different request methods correspond to different data operations.

Guess you like

Origin blog.csdn.net/weixin_49822811/article/details/113833172