REST风格【SpringBoot】

1.REST简介

REST风格【SpringBoot】_第1张图片

行为动作

REST风格【SpringBoot】_第2张图片

通常模块名使用复数,也就是加s

2.RESTful入门

@Controller
public class UserController {

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public String save() {
        System.out.println("user save");
        return "save";
    }

    /**
     * @PathVariable:表示路径中对应的占位符
     * @param id
     * @return
     */
    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable Integer id){
        System.out.println("delete ..." + id);
        return "delete";
    }

    /**
     * @RequestBody:接收前端传递给后端的json字符串
     * @param user
     * @return
     */
    @RequestMapping(value = "/users",method = RequestMethod.PUT)
    public String update(@RequestBody User user){
        System.out.println(user);
        return "put";
    }
}

1.步骤:

REST风格【SpringBoot】_第3张图片

REST风格【SpringBoot】_第4张图片

2.RequestMapping:映射

REST风格【SpringBoot】_第5张图片

3.@PathVariable:路径占位符 

REST风格【SpringBoot】_第6张图片

4.@RequesBody:接收前端传递给后端的json字符串

 5.@RequestBody,@RequestVariable,@RequestParam区别

REST风格【SpringBoot】_第7张图片

3.REST快速开发

REST风格【SpringBoot】_第8张图片

@RequestMapping("/books")
@RestController//RequestBody+Controller
public class BookController {

    @PostMapping("/save")
    public String save() {
        System.out.println("user save");
        return "save";
    }
    
    @DeleteMapping("/delete")
    public String delete(@PathVariable Integer id){
        System.out.println("delete ..." + id);
        return "delete";
    }

    @PutMapping("/update")
    public String update(@RequestBody User user){
        System.out.println(user);
        return "put";
    }

}

你可能感兴趣的:(java,mybatis)