天天看點

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){}
                 

繼續閱讀