java - typeMismatch.target spring boot 批量拒绝值 [2020-09-18T00:00:00+02:00]
问题描述
我正在尝试使用批处理将来自 CSV 文件的数据与 Spring Boot 集成,我对日期字段有疑问,因为无论使用哪种类型,它都会不断被拒绝, 这是我的代码:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String service;
private Timestamp time;
@Override
public IndProd process(final IndProd indProd) throws Exception {
String service = indProd.getService();
Timestamp time = indProd.getTime();
Long nbAppels = indProd.getNbAppels();
Integer tempsDeReponseMoyenMillisecondes = indProd.getTempsDeReponseMoyenMillisecondes();
Long volume = indProd.getVolume();
BigDecimal tempsDeReponseMoyenSecondes = indProd.getTempsDeReponseMoyenSecondes();
IndProd transformedIndProd = new IndProd(service,time,nbAppels,tempsDeReponseMoyenMillisecondes,volume,tempsDeReponseMoyenSecondes);
return transformedIndProd;
}
这是返回的错误:
引起:org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1 错误字段“时间”上对象“目标”中的字段错误:拒绝值 [2020-09-18T00:00:00+02:00 ]; 代码 [typeMismatch.target.time,typeMismatch.time,typeMismatch.java.sql.Timestamp,typeMismatch]; 参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [target.time,time]; 论据 []; 默认消息[时间]];默认消息 [无法将类型“java.lang.String”的属性值转换为属性“时间”所需的类型“java.sql.Timestamp”;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为属性“时间”所需的类型“java.sql.Timestamp”:
谢谢你的帮助
解决方案
正如错误所说,您正在尝试将 String 值分配给 Instant 变量。这意味着indProd.getTime()
试图成为字符串。
解决此问题的一种方法是将 String 重新键入回即时,这可能不是最好的解决方案,但应该可以。
Timestamp time = Timestamp.from(ZonedDateTime.parse(indProd.getTime()).toInstant());
并在IndProd
可变更改时间为字符串
推荐阅读
- linux - 在不使用正则表达式的情况下使用换行符在文件内容中使用 grep 搜索关键字
- admob - 原生广告与横幅 (AdMob):Ram 使用
- python - 如何根据最后两位数字对数字列表进行排序?
- jestjs - Jest Enzyme Mount 错误:引用一个值,但在此处用作类型 ts(2749)
- node.js - nodejs:监控任务队列的正确方法是什么?
- excel - 使用带有条件的VBA将值从一张纸复制到另一张纸
- python - 带有传递变量的 OCC gp_Pnt
- php - Laravel 雄辩的关系和数据透视表
- python - 在 / 处获取 TemplateDoesNotExist
- json - 在 Postman 中发送多个文件 - NodeJS