当时遇到这个错误的时候,也是很懵逼,感觉自己并没有写错,怎么会报这个错误,后来冷静分析了一下,才发现是接口定义的问题,= =。先上一下错误日志:
handleTypeMismatch Failed to bind request element:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer
看一下我的两个请求接口,都是GET请求:
/**查找所有课程*/
@GetMapping("/app/society/courses")
public Object userInfo(){
...
...
}
/**根据课程ID,查询课程详情*/
@GetMapping("/app/society/{course_id}")
public Object userInfo(@PathVariable("course_id")Integer courseId){
...
...
}
看到我的两个接口定义,你会不会发现了什么。其实当我请求/app/society/courses接口时,才报的这个错,但为什么会报String转Integer异常呢,是因为Spring将“courses”作为URL参数,将请求发给了/app/society/{course_id},但course_id定义的是Integer,所以会Spring会报“courses”这个字符串从String转Integer错误!!!!!!!!!!
伤心欲绝,总以为自己没有错,其实还是自己错了,= =.
好了,解决方案大家应该都很清楚了,只需要修改下第二个接口的URI就可以了,修改后的例子如下:
/**查找所有课程*/
@GetMapping("/app/society/courses")
public Object userInfo(){
...
...
}
/**根据课程ID,查询课程详情*/
@GetMapping("/app/society/{course_id}/info")
public Object userInfo(@PathVariable("course_id")Integer courseId){
...
...
}
github上有我更多的笔记:Raray-chuan (兮川) · GitHub,欢迎stars与following,如果有问题可以在issue中向我咨询
关注我的公众号,获取更多关于后端、大数据的知识