【问题标题】:typeMismatch.target spring boot batch rejected value [2020-09-18T00:00:00+02:00]typeMismatch.target spring boot 批量拒绝值 [2020-09-18T00:00:00+02:00]
【发布时间】:2020-11-20 14:40:17
【问题描述】:

我正在尝试使用批处理将 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”:没有匹配的编辑器或 找到转换策略] 在 org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:201) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在 org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在 org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:185) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE] ... 56 常用框架省略

感谢您的帮助

【问题讨论】:

    标签: java spring spring-boot spring-batch timestamp-with-timezone


    【解决方案1】:

    从错误中可以清楚地看出,indProd.getTime() 返回一个 String 值,您尝试将其分配给 Timestamp 变量。假设 indProd.getTime() 返回格式为 yyyy-MM-dd'T'HH:mm:ssXXX 的日期时间字符串,例如2020-09-18T00:00:00+02:00(如您的问题标题中所述),您应该替换

    Timestamp time = indProd.getTime();
    

    Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").parse(indProd.getTime()).getTime());
    

    注意: java.sql.Timestamp 扩展 java.util.Datejava.util 的日期时间 API 及其格式 API SimpleDateFormat 已过时且容易出错。我建议你应该完全停止使用它们并切换到modern date-time API

    使用现代日期时间 API:

    OffsetDateTime odt = OffsetDateTime.parse(indProd.getTime());
    //...
    IndProd transformedIndProd = new IndProd(service,odt,nbAppels,tempsDeReponseMoyenMillisecondes,volume,tempsDeReponseMoyenSecondes);
    

    并将实例成员声明为

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String service;
    private OffsetDateTime odt;// Change the type to OffsetDateTime
    

    通过 Trail: Date Time 了解有关现代日期时间 API 的更多信息。如果您正在为一个 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2020-09-04
        • 1970-01-01
        • 2022-11-19
        • 1970-01-01
        • 2013-03-23
        • 2011-02-05
        • 2021-07-10
        • 1970-01-01
        • 2017-01-27
        相关资源
        最近更新 更多