@RequestParam
只能接收 URL 后跟着的参数,不管是 GET 还是 POST,虽然一般只有 GET 请求才会在 URL 后边跟参数,问号 ? 后面的部分,使用 & 区分参数。
http://localhost:8080/api/user/login/test?username=2222222&pass=333333333
@RequestParam("username")String username
@RequestBody
只能接收请求体中的参数,也就是只能是 POST 请求才有请求体,GET 请求没有请求体,请求体分两种情况参数
(1)使用String接收
比如前端参数在请求体中传的是 username=18514335982&pass=12345,Content type 为 text/plain;charset=UTF-8
则后台接收到的 param 即为 username=18514335982&pass=12345 格式
@RequestBody String param
(2)使用封装的 bean 或者 JSONObject 接收
前端必须使用 JSON 格式的数据,Content-Type 必须为 application/json,请求体中参数为 {"username":"18514335982","pass":"12345"}
@RequestBody User user
@RequestBody JSONObject jsonObject
测试代码
@PostMapping("/login/test") public ResultBuilder userLogin1(@RequestHeader(Constants.ACCEPT_VERSION)String version, @RequestHeader(Constants.ACCESS_TOKEN)String token, @RequestParam("username")String username, @RequestParam("pass")String pass, @RequestBody User user){ logger.debug("username======" + username); logger.debug("pass======" + pass); logger.debug("user---username==" + user.getUsername()); logger.debug("user---pass==" + user.getPass()); return new ResultBuilder(StatusCode.SUCCESS); }