1

I am trying to read a csv file using Jackson csv Mapper and map the entries to a Pojo which has a LocalDate field. 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)

I have tried the methods given in other answers but still getting the same results, Only Annotation which works is

@JsonDeserialize(using = 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. 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

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

2 Answers2

1

In your pojo you need to set annotation
@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

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • if i have 20 Pojos with date field, do I need to write this at every POJO class? – Inderjit Chopra Jan 02 '20 at 11:57
  • Yes, you do, just like any other annotation. But you annotate a property, not the entire class. It is a property level annotation. First, try it on one and see if it works – Michael Gantman Jan 02 '20 at 12:04
  • But what if I want the serializer and deserializer work on its own without worrying about these extra annotations? I used the annotations listed in question and it worked but i want to design a more cohert and universal solution – Inderjit Chopra Jan 02 '20 at 12:10
  • I believe you will need to write a custom deserializer, but I haven't done so. So, I don't know the details. The annotations are an acceptable solution though – Michael Gantman Jan 02 '20 at 13:02
0

jackson-datatype-jsr310 is registered by default if on the classpath as stated on Springs own blog Jackson-Modules Spring Blog so no need to register it manually in a new ObjectMapper at all.

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54