[英]Fix Jackson cannot construct instance of Java.time.LocalDate
I am trying to read a csv file using Jackson csv Mapper and map the entries to a Pojo which has a LocalDate field.我正在尝试使用 Jackson csv Mapper 读取 csv 文件并将条目映射到具有 LocalDate 字段的 Pojo。 While trying to parse the file I am getting the following error
在尝试解析文件时,我收到以下错误
Cannot construct instance of
java.time.LocalDate
(no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-12-01')\\n at [Source: (com.fasterxml.jackson.dataformat.csv.impl.UTF8Reader)无法构造
java.time.LocalDate
实例(没有创建者,如默认构造,存在):没有从字符串值反序列化的字符串参数构造函数/工厂方法('2019-12-01')\\n 在 [来源:(com .fasterxml.jackson.dataformat.csv.impl.UTF8Reader)
I have tried the methods given in other answers but still getting the same results, Only Annotation which works is我已经尝试了其他答案中给出的方法,但仍然得到相同的结果,唯一有效的注释是
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonDeserialize(使用 = LocalDateDeserializer.class)
I have tried to create a bean of Object Mapper and register its module in App Configuration file I created but still no Effect.我试图创建一个 Object Mapper 的 bean 并在我创建的 App 配置文件中注册它的模块,但仍然没有效果。 Kindly someone help me
请有人帮助我
Pojo波乔
@Data
class TestImport{
@JsonProperty("start_date")
LocalDate startDate;
}
Object Mapper Configuration:对象映射器配置:
@Bean
@Primary
public ObjectMapper objectMapper(){
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}
File Reading Code:文件读取代码:
public <T> List<T> read(Class<T> Entity, MultipartFile file) throws Exception{
InputStream inputStream = new BufferedInputStream(file.getInputStream());
CsvSchema schema = csvMapper.schemaFor(Entity).withHeader().withColumnReordering(true);
ObjectReader reader = csvMapper.readerFor(Entity).with(schema);
List<T> result= reader.<T>readValues(inputStream).readAll();
inputStream.close();
return result;
}
Pom.xml xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.10.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.millij/poi-object-mapper -->
<dependency>
<groupId>io.github.millij</groupId>
<artifactId>poi-object-mapper</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
In your pojo you need to set annotation在您的 pojo 中,您需要设置注释
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
For more details look at the answer for question Spring Data JPA - ZonedDateTime format for json serialization @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
有关更多详细信息,请查看问题Spring Data JPA - ZonedDateTime format for json serialization的答案
jackson-datatype-jsr310
如果在 Springs 自己的博客Jackson-Modules Spring 博客中所述的类路径上,则默认注册,因此根本不需要在新的ObjectMapper
中手动注册它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.