There was an unexpected error (type=Bad Request, status=400).

喜欢ヅ旅行 2024-04-01 17:47 287阅读 0赞

1.问题描述

本来是在学习@RequestParam 这个注解,前后端代码完善后就在浏览器里进行了测试,结果报了 400 的错。

8ee82b14a99941e29c6c1ee2368737e5.png

前端的请求链接如下:

  1. <a href="/requestParam?name=张三&hobby=run&hobby=teach">@RequestParam 【GET请求带参数】</a><hr/>

2.分析

首先得知道 400 这个状态码是啥意思:400 ,bad request意思是“错误的请求”;

所以是请求方式有问题吗? 我寻思这里的GET请求也是符合URL语法的,所以问题肯定出在后端Controller方法上,即后端要求的属性名与前端实际请求携带的属性名不一致,最终导致问题产生。

3.解决

改一下后端接口的代码就行了

错误的写法

注意这里写的是“hobbby”, 本来应该是“hobby”,也就是说多了一个 “b”

  1. @GetMapping("/requestParam")
  2. public String requestParam(@RequestParam("name") String username,
  3. @RequestParam("hobbby") List<String> hobbies){
  4. return "用户名:"+username + "\t 爱好:"+hobbies;
  5. }

正确的写法

  1. @GetMapping("/requestParam")
  2. public String requestParam(@RequestParam("name") String username,
  3. @RequestParam("hobby") List<String> hobbies){
  4. return "用户名:"+username + "\t 爱好:"+hobbies;
  5. }

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读