Java - how to read CSV file into java object using Jackson CSV library - complex example with BigDecimal and enum

14 points
Created by:

1. Overview

In this post we will read CSV file into java pojo object using Jackson CSV processor.

This example contains BigDecimal and enum as ComplexUserDto properties.

To use Jackson CSV library we need to add jackson-dataformat-csv dependency to pom.xml.


2. Read CSV file into java object

Below we have all classes we need to read csv file. Ensure to create csv file on your disc and set correct path in ComplexUserCsvReader class.

To run this example copy to your project those 3 files:

  1. ComplexUserCsvReader
  2. ComplexUserDto
  3. users.csv

2.1 Logic

Logic which reads CSV file into java ComplexUserDto pojo object with Jackson CSV library.

Usage of classes:

  • CsvMapper + CsvSchema from jackson CSV library
  • MappingIterator from jackson databind library
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.util.List;

public class ComplexUserCsvReader {

    public static void main(String[] args) throws IOException {

        // set correct path to csv file on your disc
        File csvFile = new File("C:\\csv_tests\\complex_users.csv");

        CsvMapper csvMapper = new CsvMapper();

        CsvSchema csvSchema = csvMapper

        MappingIterator<ComplexUserDto> complexUsersIter = csvMapper

        List<ComplexUserDto> complexUsers = complexUsersIter.readAll();



ComplexUserDto{id=1, name='Ann', age=30, favouriteFruit=ORANGE, money=10001.222}
ComplexUserDto{id=2, name='Seth', age=25, favouriteFruit=APPLE, money=5550.788}
ComplexUserDto{id=3, name='Tom', age=27, favouriteFruit=BANANA, money=400.999}

2.2 User Pojo object - ComplexUserDto

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.math.BigDecimal;

@JsonPropertyOrder({"id", "name", "age", "favouriteFruit", "amount"})
public class ComplexUserDto {

    enum Fruit {

    private Long id;
    private String name;
    private Integer age;
    private Fruit favouriteFruit;
    private BigDecimal money;

    public ComplexUserDto() {


    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Integer getAge() {
        return age;

    public void setAge(Integer age) {
        this.age = age;

    public Fruit getFavouriteFruit() {
        return favouriteFruit;

    public void setFavouriteFruit(Fruit favouriteFruit) {
        this.favouriteFruit = favouriteFruit;

    public BigDecimal getMoney() {
        return money;

    public void setMoney(BigDecimal money) { = money;

    public String toString() {
        return "ComplexUserDto{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", favouriteFruit=" + favouriteFruit +
                ", money=" + money +

2.3 Complex user CSV file

Save this file on disc as 'complex_users.csv' and change location in ComplexUserCsvReader class.



  1. jackson-dataformat-csv - maven
  2. Comma-separated values (CSV) - wiki
  3. CsvMapper - JavaDoc
  4. CsvSchema - JavaDoc
  5. MappingIterator - JavaDoc
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé