SpringMVC - REST风格介绍已经RESTful简化开发

REST

REST基本介绍

REST (Representational State Transfer), 表现形式状态转换(访问网络资源的风格)

传统风格资源描述形式

  • http://localhost/user/getById?id=1
  • http://localhost/user/saveUser

REST风格描述形式

  • http://localhost/user/1
  • http://localhost/user

REST优点:

隐藏资源的访问行为,无法通过地址得知对资源是何种操作

书写简化

REST风格介绍:

按照REST风格访问资源时使用行为动作区分对资源进行了何种操作

  • http://localhost/users 查询全部用户信息 GET(查询)
  • http://localhost/users/1 查询指定用户信息 GET(查询)
  • http://localhost/users 添加用户信息 POST(新增/保存)
  • http://localhost/users 修改用户信息 PUT(修改/更新)
  • http://localhost/users/1 删除用户信息 DELETE(删除)

根据REST风格对资源进行访问称为RESTful

注意事项:

上述行为是约定方式,约定不是规范,可以打破,所以称REST风格,而不是REST规范

描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts……

RESTful快速入门

1. 设定http请求动作(请求方式)

通过@RequestMapping注解的, method属性

@Controller
public class UserController {
    
    
    // 新增/保存 指定请求行为: POST
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user) {
    
    
        System.out.println("user save..." + user);
        return "{'module': 'user save'}";
    }

    // 删除 指定请求行为: DELETE
    @RequestMapping(value = "/users", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(Integer id) {
    
    
        System.out.println("user delete..." + id);
        return "{'module': 'user delete'}";
    }
}

2. 设定请求参数(路径变量)

@PathVariable注解, 写在形参变量的前面, 绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应

设置完成后可以在路径中携带id访问: http://localhost:80/users/1

@Controller
public class UserController {
    
    
    // 定义路径参数
    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
  	// 从路径参数中取值
    public String delete(@PathVariable Integer id) {
    
    
        System.out.println("user delete..." + id);
        return "{'module': 'user delete'}";
    }
}

@RequestBody @RequestParam @PathVariable 对比:

区别:

@RequestParam用于接收url地址传参或表单传参

@RequestBody用于接收json数据

@PathVariable用于接收路径参数,使用{参数名称}描述路径参数

应用:

后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广

如果发送非json格式数据,选用@RequestParam接收请求参数

采用RESTful进行开发,当参数数量较少时,例如1个,可以采用@PathVariable接收请求路径变量,通常用于传递id值

RESTful快速开发

使用RESTful开发, 我们可以发现有许多重复的代码

例如下面代码: 每一个处理器都需要写value = "/users"以及@ResponseBody;

@Controller
public class UserController {
    
    
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user) {
    
    
        System.out.println("user save..." + user);
        return "{'module': 'user save'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user) {
    
    
        System.out.println("user update..." + user);
        return "{'module': 'user update'}";
    }
}

简化一: 将公共的路径提取到处理器所在类上

@Controller
@RequestMapping("/users")
public class UserController {
    
    
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user) {
    
    
        System.out.println("user save..." + user);
        return "{'module': 'user save'}";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id) {
    
    
        System.out.println("user delete..." + id);
        return "{'module': 'user delete'}";
    }
}

简化二: 将每个处理器都有的@RequestBody注解提取到处理器所在类上

@Controller
@RequestMapping("/users")
@ResponseBody
public class UserController {
    
    
    @RequestMapping(method = RequestMethod.POST)
    public String save(@RequestBody User user) {
    
    
        System.out.println("user save..." + user);
        return "{'module': 'user save'}";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable Integer id) {
    
    
        System.out.println("user delete..." + id);
        return "{'module': 'user delete'}";
    }
}

简化三: @RestController注解

@RestController等同于@ResponseBody加上@ResponseBody注解组合起来的功能

@RestController
@RequestMapping("/users")
public class UserController {
    
    
    @RequestMapping(method = RequestMethod.POST)
    public String save(@RequestBody User user) {
    
    
        System.out.println("user save..." + user);
        return "{'module': 'user save'}";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable Integer id) {
    
    
        System.out.println("user delete..." + id);
        return "{'module': 'user delete'}";
    }
}

简化四: 通过@GetMapping @PostMapping @PutMapping @DeleteMapping注解替代method = RequestMethod.XXX

设置当前控制器方法请求访问路径与请求动作,每种对应一个请求动作,例如@GetMapping对应GET请求;

默认属性value:请求访问路径

@RestController
@RequestMapping("/users")
public class UserController {
    
    
    // 新增/保存 指定请求行为: POST
    @PostMapping
    public String save(@RequestBody User user) {
    
    
        System.out.println("user save..." + user);
        return "{'module': 'user save'}";
    }

    // 删除 指定请求行为: DELETE
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id) {
    
    
        System.out.println("user delete..." + id);
        return "{'module': 'user delete'}";
    }

    // 修改/更新 指定请求行为: PUT
    @PutMapping
    public String update(@RequestBody User user) {
    
    
        System.out.println("user update..." + user);
        return "{'module': 'user update'}";
    }

    // 查询全部 指定请求行为: GET
    @GetMapping
    public String selectAll() {
    
    
        System.out.println("user selectAll...");
        return "{'module': 'user selectAll'}";
    }

    // 查询根据id 指定请求行为: GET
    @GetMapping("/{id}")
    public String selectById(@PathVariable Integer id) {
    
    
        System.out.println("user selectById..." + id);
        return "{'module': 'user selectById'}";
    }
}

猜你喜欢

转载自blog.csdn.net/m0_71485750/article/details/128021554