简体   繁体   English

修复 Jackson 无法构造 Java.time.LocalDate 的实例

[英]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.

相关问题 无法构造`java.time.LocalDate` 的实例 - Spring boot、elasticseach、jackson - Cannot construct instance of `java.time.LocalDate` - Spring boot, elasticseach, jackson com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`java.time.LocalDate的实例 - com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate Jackson 使用 Spring 引导 2 和 Kotlin 反序列化,无法构造 `java.time.LocalDate` 的实例 - Jackson deserialization with Spring Boot 2 and Kotlin, Cannot construct instance of `java.time.LocalDate` 在 java 日期格式:com.fasterxml.jackson.databind.exc.InvalidDefinition.timeException:无法构造实例的`LocalDatejava. - in java date format: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` 无法构造 `java.time.LocalDate` 的实例(没有创建者,如默认构造,存在):无法从对象值反序列化 - Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): cannot deserialize from Object value Hibernate 4使用java.time.LocalDate和DATE()构造 - Hibernate 4 with java.time.LocalDate and DATE() construct 使用Jackson将java.time.localdate序列化为json - Serialize java.time.localdate into json using Jackson 无法解决 android studio 中的符号“java.time.LocalDate”错误 - cannot resolve symbol 'java.time.LocalDate' error in android studio 无法反序列化“java.time.LocalDate”类型的值 - Cannot deserialize value of type `java.time.LocalDate` setCellValue(String value) throws java: cannot access java.time.LocalDate class file for java.time.LocalDate not found - setCellValue(String value) throws java: cannot access java.time.LocalDate class file for java.time.LocalDate not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM