zl程序教程

您现在的位置是:首页 >  后端

当前栏目

异常Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;

JAVAFailed异常 string to of type value
2023-09-11 14:15:14 时间

首先看错误能看出来类型转换错误,我是在Controller层在地址上以斜杠的方式来取参数的时候报错的。

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: 
Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; 

代码:

@RequestMapping(value = "/deleteHealthExam/{healthExamId}/{healthExamDate}", method = RequestMethod.DELETE)
	public ResultDto deleteHealthExam(@PathVariable String healthExamId,
			@PathVariable("healthExamDate") Date healthExamDate) {
		return healthExamService.deleteHealthExam(healthExamId,healthExamDate);
	}

原因:
healthExamDate我是以Date类型去接参数的,但是他默认是字符串类型,这里得需要类型转换一下。

解决方案:
添加@DateTimeFormat注解,让他类型转换成Date类型

@PathVariable("healthExamDate") 
@DateTimeFormat(pattern="yyyy-MM-dd") 
Date healthExamDate