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>