springmvc REST风格

@RestController注解:结合了@Controller和@ResponseBody注解,用于创建RESTful控制器。

@RestController
public class MyRestController {
    @GetMapping("/users")
    public List<User> getUsers() {
        // 返回用户列表
    }

    @PostMapping("/users")
    public User createUser(@RequestBody User user) {
        // 创建新用户
        return user;
    }

    @PutMapping("/users/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        // 更新用户
        return user;
    }

    @DeleteMapping("/users/{id}")
    public void deleteUser(@PathVariable Long id) {
        // 删除用户
    }
}

@RequestMapping注解:用于映射请求URL到控制器方法。

@PathVariable注解:用于从URL路径中提取变量。

@RequestBody注解:用于从请求体中提取数据。

posted @ 2025-02-02 18:26  Look_Back  阅读(14)  评论(0)    收藏  举报