首页 > 解决方案 > 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”:

谢谢你的帮助

标签: javaspringspring-bootspring-batchtimestamp-with-timezone

解决方案


正如错误所说,您正在尝试将 String 值分配给 Instant 变量。这意味着indProd.getTime()试图成为字符串。

解决此问题的一种方法是将 String 重新键入回即时,这可能不是最好的解决方案,但应该可以。

Timestamp time = Timestamp.from(ZonedDateTime.parse(indProd.getTime()).toInstant());

并在IndProd可变更改时间为字符串


推荐阅读