Spring Boot Failed to convert value of type ‘xxx‘ to required type ‘xxx‘问题解决
问题描述:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type ‘java.lang.String’ to required type ‘java.lang.Long’;
问题分析:
1、使用Swagger调试地址栏传参的接口时,参数类型paramType设置为query,导致调用接口报错。
@DeleteMapping("delete/{id}")
@ApiOperation(value = "删除")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "ID", paramType = "query", dataType = "Long", required = true)
})
public String delete(@PathVariable("id") Long id){
return "";
}
解决办法:将paramType设置为path,代表地址栏传参。
@DeleteMapping("delete/{id}")
@ApiOperation(value = "删除")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "ID", paramType = "path", dataType = "Long", required = true)
})
public String delete(@PathVariable("id") Long id){
return "";
}
2、后端接口参数类型为Date,但是前端传参类型为String。
public String testDate(@RequestParam Date time) {
return DateUtil.format(time,"yyyy-MM-dd");
}
解决办法:
(1)后端接口参数类型改为String,前端传参不变。
public String testDate(@RequestParam String time) {
return time;
}
(2)后端接口参数类型添加@DateTimeFormat,将固定格式的字符串转成Date,前端传参不变。
public String testDate(@RequestParam @org.springframework.format.annotation.DateTimeFormat(pattern = "yyyy-MM-dd") Date time) {
return DateUtil.format(time,"yyyy-MM-dd");
}
还没有评论,来说两句吧...