天天看点

Http请求参数传递常见问题

Http参数传递

1. get请求/delete请求

  • 路径传参__直接拼接参数
    • URL : http://localhost:9090/user/1234
    • 使用@PathVariable进行参数声明
      @GetMapping("/{uid}")
      public void findByUid(@PathVariable("uid")Integer uid){}
                 
  • 路径传参__使用 ? 进行参数拼接
    • URL : http://localhost:9090/user?username=jack&password=1234
    • 接收单个参数,使用@RequestParam注解进行声明
      @GetMapping("/findAll")
          public void findAll(@RequestParam("username") String  username){}
                 
    • 接收对象,不能使用@RequestParam注解,Http会自动进行数据封装
      @GetMapping
          public void findByUsernameAndPassword(User user){}
                 
      • 注意 : 使用@RequestParam进行参数声明会报错,找不到对应的参数

2. Post请求/Put请求

  • 使用请求体进行参数传递,请求体为json数据类型
    • 后台使用@RequestBody进行参数接收
      @PostMapping
          public void addUser(@RequestBody User user){}
                 

继续阅读