将日期字段与 jackson-dataformat-csv 一起使用时因 InvalidFormatException 而失败

Failing with InvalidFormatException when using date fields with jackson-dataformat-csv

我正在尝试使用 jackson-dataformat-csv 中的 CSVMapper 实用程序来解析 CSV 文件并创建 POJO 记录以插入到 mysql 数据库。

这样做时,如果日期信息作为 CSV 中的列之一存在并映射到 POJO 中的日期字段,我会遇到问题。

通过读取 CSV 文件并将其转换为 POJO,我有了以下代码。

Bug.java

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Bug implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @JsonProperty
    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private Long Num;

    @JsonProperty("Show Bug")
    private String URL;

    @JsonProperty
    private String Subject;

    @JsonProperty
    private String Customer;

    @JsonProperty
    private String Tags;

    @JsonProperty
    private String St;

    @JsonProperty("Rep. Release")
    private String ReportedRelease;

    @JsonProperty("Reported")
    private Date reportedDate;

    @JsonProperty
    private String Sev;

    @JsonProperty
    private String Assignee;

    @JsonProperty
    private String Component;

    public Bug() {}

        /* getters and setters */

控制器代码


@PostMapping(value = "/upload", consumes = "multipart/form-data")
    public void uploadMultipart(@RequestParam("file") MultipartFile file) throws IOException {

try {

            CsvSchema bootstrap = CsvSchema.builder().setUseHeader(true)
                        .addColumn("Sl No.", CsvSchema.ColumnType.NUMBER)
                        .addColumn("Num", CsvSchema.ColumnType.NUMBER)
                        .addColumn("Show Bug", CsvSchema.ColumnType.STRING)
                        .addColumn("Customer", CsvSchema.ColumnType.STRING)
                        .addColumn("Rep. Release", CsvSchema.ColumnType.STRING)
                        .addColumn("Reported")
                        .addColumn("Component", CsvSchema.ColumnType.STRING)
                        .addColumn("Assignee", CsvSchema.ColumnType.STRING)
                        .addColumn("Sev", CsvSchema.ColumnType.STRING)
                        .addColumn("St", CsvSchema.ColumnType.STRING)
                        .addColumn("Tags", CsvSchema.ColumnType.STRING)
                        .addColumn("Subject", CsvSchema.ColumnType.STRING)
                        .build().withHeader();

            CsvMapper csvMapper = new CsvMapper();

            MappingIterator<Bug> bugInfo = 
                    csvMapper.readerFor(Bug.class).with(bootstrap).readValues(file.getInputStream());

            System.out.println("************************ Bug Information ************************");

            while(bugInfo.hasNext()) {
                Bug bugRec = bugInfo.next();
                System.out.println("Customer : " + bugRec.getCustomer());
                System.out.println("URL : " + bugRec.getURL());
            }


} catch(IOException exception) {
            throw exception;
        }
    }

这确实工作了一段时间,我能够在 while 循环中打印记录,然后将记录插入到 mysql table.

不确定代码中发生了什么变化,现在我遇到了以下问题


com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.sql.Date from String value ("20-MAY-19"): not a valid representation (error: Failed to parse Date value '20-MAY-19': Can not parse date "20-MAY-19": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: com.fasterxml.jackson.dataformat.csv.impl.UTF8Reader@411358fc; line: 2, column: 147] (through reference chain: com.app.oracle.OMBugAnalyzerServices.entity.Bug["Reported"])
    at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:74) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1021) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:788) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:175) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$SqlDateDeserializer.deserialize(DateDeserializers.java:284) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.std.DateDeserializers$SqlDateDeserializer.deserialize(DateDeserializers.java:269) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:490) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:260) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.MappingIterator.nextValue(MappingIterator.java:277) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:192) ~[jackson-databind-2.7.4.jar:2.7.4]
    at com.app.oracle.OMBugAnalyzerServices.controller.BugAnalyzerController.uploadMultipart(BugAnalyzerController.java:129) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]

如以下异常所述,

com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.sql.Date from String value ("20-MAY-19"): not a valid representation (error: Failed to parse Date value '20-MAY-19': Can not parse date "20-MAY-19": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

可能,您更改了以下内容。

@JsonProperty("Reported")
private Date reportedDate;

您可以在 jackson 中设置格式模式,如下所示:

@JsonFormat(pattern="yy-MM-dd")
@JsonProperty("Reported")
private Date reportedDate;