[英]Fail to convert String @RequestParam to javasql.Timestamp
I have a unit test that checks a get request that have a TimeStamp parameter.我有一个单元测试来检查具有 TimeStamp 参数的获取请求。
The UT mocks a request with the current TimeStemp to String. UT 将当前 TimeStemp 的请求模拟为 String。
Timestamp myTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
MockHttpServletRequestBuilder request =get(REST_PATH).param("horarioFim", mytimeStamp.toString());
In my controller I have在我的 controller 我有
@RequestMapping(method = GET)
public Iterable<AuditVersionModel> search(@RequestParam(required = false) Timestamp horarioFim)
TimeStamp is imported from java.sql.TimeStamp时间戳从 java.sql.TimeStamp 导入
The test fails and I have this information.测试失败,我有这个信息。
MockHttpServletResponse:
Status = 500
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {"timestamp":"2020-05-07T23:58:48.298+0000","status":999,"error":"None","message":"Failed to convert value of type 'java.lang.String' to required type 'java.sql.Timestamp'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.sql.Timestamp] for value '2020-05-07 20:58:48.201'; nested exception is java.lang.IllegalArgumentException: Invalid format: \"2020-05-07 20:58:48.201\" is malformed at \" 20:58:48.201\""}
Forwarded URL = null
Redirected URL = null
Cookies = []
I tried to use @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
and other variations without success, it is always the same error.我尝试使用
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
和其他变体但没有成功,它总是同样的错误。
OBS1:This project is a migration from Spring Boot 1.5 to 2.0, the code is simplified. OBS1:本项目是从Spring Boot 1.5迁移到2.0,代码简化。 OBS2: This is a legacy system, the test is the only way to validate the request at the moment.
OBS2:这是一个遗留系统,测试是目前验证请求的唯一方法。
Assuming the deserialization framework you are using is expecting the date format to be in ISO format, the date passed in is not in ISO format.假设您使用的反序列化框架期望日期格式为 ISO 格式,则传入的日期不是 ISO 格式。 I tried passing in "2020-05-07T23:58:48.298+0000" to OffsetDateTime.parse() API and it threw an exception.
我尝试将“2020-05-07T23:58:48.298+0000”传递给OffsetDateTime.parse() API 并引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.