gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:05:27 24 4
gpt4 key购买 nike

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

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

我通过读取 CSV 文件并将其转换为 POJO 来编写以下代码。

错误.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 */

Controller 代码


@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 表中。

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


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]

最佳答案

你可以像下面这样在 jackson 中设置格式模式:

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

关于java - 将日期字段与 jackson-dataformat-csv 一起使用时因 InvalidFormatException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com