REST简介
-
REST,表现形式状态转换
-
REST风格描述资源的访问形式
-
按照REST风格来访问资源叫做RESTful
-
按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
- POST ====>增加
- DELETE===>删除
- PUT=====>修改
- GET=====>查询
RESTful快速开发(标准开发)
- 实体类User
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
- UserController类
//@Controller
//@ResponseBody
@RestController
@RequestMapping("/users")
public class UserController {
// @RequestMapping(value = "/users",method = RequestMethod.POST)
@PostMapping
public String add(@RequestBody User user){
System.out.println("add user ===>"+user);
return "{'module':'add user'}";
}
// @RequestMapping(value = "/users",method = RequestMethod.DELETE)
@DeleteMapping("{id}")
public String delete(@PathVariable Integer id){
System.out.println("delete user "+id);
return "{'module':'delete user'}";
}
@PutMapping
public String update(@RequestBody User user){
System.out.println("update user "+user);
return "{'module':'update user'}";
}
@GetMapping
public String selectAll(){
System.out.println("select all users");
return "{'module':'selectAll users'}";
}
@GetMapping("/{id}")
public String selectById(@PathVariable Integer id){
System.out.println("select users"+id);
return "{'module':'selectById users'}";
}
}
- 简化开发
- 使用@RestController代替(@Controller和@ResponseBody)的合体
- 将@RequestMapping("/users")写在类上面
- 使用@PostMapping、 @DeleteMapping、@PutMapping、@GetMapping精确请求方式