Spring MVC - Rest style

REST(Representational State Transfer)

  • When we want to represent a network resource, we can use two ways:
    we use 查询id为1的用户信息and保存用户信息举例
  • Traditional Style Resource Description Form
    • http://localhost/user/getById?id=1
    • http://localhost/user/saveUser
  • REST style description form
    • http://localhost/user/1
    • http://localhost/user

The comparison is obvious. In the traditional way, one request url corresponds to one operation. This is not only troublesome, but also unsafe, because we can roughly know what kind of operation the url implements by reading the request url address. In the description of REST style, the request address becomes simpler, and the specific function of the URL cannot be guessed just by looking at the request URL. To sum up, there are only two advantages:

  • Hiding the resource access behavior, it is impossible to know what kind of operation is performed on the resource through the address
  • writing simplification

Then we have a new problem. The same url address can be added, modified or queried, so how do we distinguish what the request is.

  • Use when accessing resources in a REST styleBehaviorDistinguish what operations are performed on resources
    • http://localhost/usersQuery all user information GET (query)
    • http://localhost/users/1Query specified user information GET (query)
    • http://localhost/usersAdd user information POST (add/save)
    • http://localhost/usersModify user information PUT (modify/update)
    • http://localhost/users/1Delete user information DELETE (delete)

There are many ways to request, but there are four commonly used ones, namely GET: for query, POST: for adding, PUT: for modifying, and DELETE: for deleting.

CAUTION CAUTION CAUTION CAUTION CAUTION

  • The above-mentioned behavior is an agreed method, and the agreement is not a specification and can be broken, so it is called REST style, not REST specification
    • REST provides a corresponding architecture. Designing projects according to this architecture can reduce the complexity of development and improve the scalability of the system.
    • REST stipulates that GET/POST/PUT/DELETE is aimed at query/addition/modification/deletion, but if we have to use GET request to delete, this can be achieved by running the program
  • But if most people follow this style, the code you write will be a bit baffling for others to read.
  • The name of the description module is usually described in plural, that is, in the format of adding s, indicating such resources instead of a single resource, such as: users, books, accounts...

Now that we know what the REST style is, we now introduce a new concept RESTful: accessing resources according to the REST style is calledRESTful

Compare the request. One is placed in the URL path of the page, and the other is modified by using some request parameter methods.
insert image description here

name @PathVariable
type Formal parameter annotation
Location SpringMVC controller method parameter definition front
effect The relationship between binding path parameters and processor method parameters requires a one-to-one correspondence between path parameter names and formal parameter names

Regarding receiving parameters, three annotations @RequestBody, @RequestParam,@PathVariable

  • the difference
    • @RequestParam is used to receive url address or form parameters
    • @RequestBody is used to receive json data
    • @PathVariable is used to receive path parameters, use {parameter name} to describe path parameters
  • application
    • In the later stage of development, when sending more than one request parameter, the json format is the main format, and @RequestBody is widely used
    • If you send data in non-json format, use @RequestParam to receive request parameters
    • Use RESTful for development. When the number of parameters is small, such as 1, you can use @PathVariable to receive the request path variable, which is usually used to pass the id value

In fact, our actual development of the REST style is not so cumbersome. Let’s look at an example and it will be obvious:

package com.taro.controller;

import com.taro.domain.Book;
import org.springframework.web.bind.annotation.*;

//@Controller
//@ResponseBody配置在类上可以简化配置,表示设置当前每个方法的返回值都作为响应体
//@ResponseBody
@RestController     //使用@RestController注解替换@Controller与@ResponseBody注解,简化书写
@RequestMapping("/books")
public class BookController {
    
    

//    @RequestMapping( method = RequestMethod.POST)
    @PostMapping        //使用@PostMapping简化Post请求方法对应的映射配置
    public String save(@RequestBody Book book){
    
    
        System.out.println("book save..." + book);
        return "{'module':'book save'}";
    }

//    @RequestMapping(value = "/{id}" ,method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")     //使用@DeleteMapping简化DELETE请求方法对应的映射配置
    public String delete(@PathVariable Integer id){
    
    
        System.out.println("book delete..." + id);
        return "{'module':'book delete'}";
    }

//    @RequestMapping(method = RequestMethod.PUT)
    @PutMapping         //使用@PutMapping简化Put请求方法对应的映射配置
    public String update(@RequestBody Book book){
    
    
        System.out.println("book update..."+book);
        return "{'module':'book update'}";
    }

//    @RequestMapping(value = "/{id}" ,method = RequestMethod.GET)
    @GetMapping("/{id}")    //使用@GetMapping简化GET请求方法对应的映射配置
    public String getById(@PathVariable Integer id){
    
    
        System.out.println("book getById..."+id);
        return "{'module':'book getById'}";
    }

//    @RequestMapping(method = RequestMethod.GET)
    @GetMapping             //使用@GetMapping简化GET请求方法对应的映射配置
    public String getAll(){
    
    
        System.out.println("book getAll...");
        return "{'module':'book getAll'}";
    }
}

name @RestController
type class annotation
Location Above the controller class definition of RESTful development based on SpringMVC
effect Set the current controller class to RESTful style,
which is equivalent to the combination of @Controller and @ResponseBody annotations
name @GetMapping @PostMapping @PutMapping @DeleteMapping
type method annotation
Location SpringMVC-based RESTful development above the controller method definition
effect Set the current controller method request access path and request action, each corresponding to a request action,
for example, @GetMapping corresponds to a GET request
related attributes value (default): request access path

Guess you like

Origin blog.csdn.net/weixin_45696320/article/details/130296315