There was an unexpected error (type=Bad Request, status=400).
1.问题描述
本来是在学习@RequestParam 这个注解,前后端代码完善后就在浏览器里进行了测试,结果报了 400 的错。
前端的请求链接如下:
<a href="/requestParam?name=张三&hobby=run&hobby=teach">@RequestParam 【GET请求带参数】</a><hr/>
2.分析
首先得知道 400 这个状态码是啥意思:400 ,bad request意思是“错误的请求”;
所以是请求方式有问题吗? 我寻思这里的GET请求也是符合URL语法的,所以问题肯定出在后端Controller方法上,即后端要求的属性名与前端实际请求携带的属性名不一致,最终导致问题产生。
3.解决
改一下后端接口的代码就行了
错误的写法
注意这里写的是“hobbby”, 本来应该是“hobby”,也就是说多了一个 “b”
@GetMapping("/requestParam")
public String requestParam(@RequestParam("name") String username,
@RequestParam("hobbby") List<String> hobbies){
return "用户名:"+username + "\t 爱好:"+hobbies;
}
正确的写法
@GetMapping("/requestParam")
public String requestParam(@RequestParam("name") String username,
@RequestParam("hobby") List<String> hobbies){
return "用户名:"+username + "\t 爱好:"+hobbies;
}
还没有评论,来说两句吧...