dyc87112 / SpringBoot-Learning

《Spring Boot基础教程》,2.x版本持续连载中!点击下方链接直达教程目录!
https://blog.didispace.com/spring-boot-learning-2x/
15.74k stars 4.88k forks source link

使用Swagger时出现MethodArgumentTypeMismatchException #4

Closed Jack2code closed 7 years ago

Jack2code commented 7 years ago

当调用url中包含参数的restful接口时,会报错,原因是指定为Long类型的参数在请求时被识别为String,导致类型不匹配报错。

以获取用户详细信息接口为例: 在SwaggerUI上输入value为1,然后try it out,结果是:

{
  "timestamp": 1488971240704,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
  "message": "Failed to convert value of type [java.lang.String] to required type [java.lang.Long]; nested exception is java.lang.NumberFormatException: For input string: \"{id}\"",
  "path": "/users/%7Bid%7D"
}
Jack2code commented 7 years ago

问题已解决。需要在API注解中添加参数类型paramType,否则默认为body参数。此处应该是path参数。

@ApiImplicitParam(name = "num", value = "?????", required = true, dataType = "Long", paramType="path")