gpt4 book ai didi

java - 修复 Jackson 无法构造 Java.time.LocalDate 实例的问题

转载 作者:行者123 更新时间:2023-12-01 19:05:36 29 4
gpt4 key购买 nike

我正在尝试使用 Jackson csv Mapper 读取 csv 文件并将条目映射到具有 LocalDate 字段的 Pojo。在尝试解析文件时出现以下错误

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)

我已经尝试过其他答案中给出的方法,但仍然得到相同的结果,唯一有效的注释是

@JsonDeserialize(using = LocalDateDeserializer.class)

我尝试创建一个对象映射器的bean,并将其模块注册到我创建的应用程序配置文件中,但仍然没有效果。请有人帮助我

波乔

@Data
class TestImport{

@JsonProperty("start_date")
LocalDate startDate;
}

对象映射器配置:

 @Bean
@Primary
public ObjectMapper objectMapper(){
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}

文件读取代码:

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

 <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>

最佳答案

在你的pojo中你需要设置注释
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd")有关更多详细信息,请参阅问题 Spring Data JPA - ZonedDateTime format for json serialization 的答案

关于java - 修复 Jackson 无法构造 Java.time.LocalDate 实例的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59562274/

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