javadozersupercsv

Reading nested POJO with super-CSV


I was trying to load a nested POJO with CSV . For some error I couldnt make it work.

Please find my code below. The below class Person contains two POJO field address and attribs

public class Person
{
    public String          firstname;
    public String          lastname;
    public PersonAttribute attribs;
    public Address         address;
    //Getters and setters
}


PersonAttribute class 

{
   public int height;
   public int weight;
   //Getters and setters
}

Address class

public class Address
{
   public String      city;
   public String      zipCode;
   public GeoLocation geoLocation;

//Getters and setters }

GeoLocation

public class GeoLocation
{
    private String latitude;
    private String longitude;

//Getters and setters }

And now I am trying to read the CSV and populate person class . I am not able to make it work. Here is my code

public class ReadWithCsvDozerBeanReader
{

    private static final String        CSV                      = "firstname, lastname, height, weight, city,zipcode,latitude,longitude\n"
        + "bill,smith,180,200,ABC,123,48.8525525,2.3417763\n" + "james,smith,192,250,DEF,456,48.852129,2.3355093";

    private static final int           ATT_START_INDEX          = 2;

    private static final int           ADDRESS_START_INDEX      = 4;

    private static final int           GEO_LOCATION_START_INDEX = 6;

    // custom preferences required because CSV contains spaces that aren't part
    // of the data
    private static final CsvPreference PREFS                    = new CsvPreference.Builder(
            CsvPreference.STANDARD_PREFERENCE).surroundingSpacesNeedQuotes(true).build();

    public static void main (String[] args) throws IOException
    {
        readWithCsvDozerBeanReader(new StringReader(CSV));

    }

    private static void readWithCsvDozerBeanReader (final Reader reader) throws IOException
    {
        ICsvDozerBeanReader beanReader = null;
        try
        {
            beanReader = new CsvDozerBeanReader(reader, PREFS);

            final String[] header = beanReader.getHeader(true);

            // set up the field mapping, processors and hints dynamically
            final String[] fieldMapping = new String[header.length];
            final CellProcessor[] processors = new CellProcessor[header.length];
            final Class<?>[] hintTypes = new Class<?>[header.length];
            for (int i = 0; i < header.length; i++)
            {
                if (i < ATT_START_INDEX)
                {
                    // normal mappings
                    fieldMapping[i] = header[i];
                    processors[i] = new NotNull();
                    hintTypes[i] = Person.class;
                }
                else if (i < ADDRESS_START_INDEX)
                {
                    // attribute mappings
                    fieldMapping[i] = header[i];
                    processors[i] = new NotNull();
                    hintTypes[i] = PersonAttribute.class;
                }
                else if (i < GEO_LOCATION_START_INDEX)
                {

                    // Address mappings
                    fieldMapping[i] = header[i];
                    hintTypes[i] = Address.class;

                }
                else
                {
                    fieldMapping[i] = header[i];
                    hintTypes[i] = GeoLocation.class;
                }

            }

            beanReader.configureBeanMapping(Person.class, fieldMapping, hintTypes);

            Person person;
            while ((person = beanReader.read(Person.class, processors)) != null)
            {
                System.out.println(String.format("lineNo=%s, rowNo=%s, person=%s", beanReader.getLineNumber(),
                        beanReader.getRowNumber(), person));
            }

        }
        finally
        {
            if (beanReader != null)
            {
                beanReader.close();
            }
        }
    }

}

I am getting the below error Exception in thread "main" org.dozer.MappingException: No read or write method found for field (height) in class


Solution

  • I got it to work. The problem was in field mapping.

     private static void readWithCsvDozerBeanReader (final Reader reader) throws IOException
    {
        ICsvDozerBeanReader beanReader = null;
        try
        {
            beanReader = new CsvDozerBeanReader(reader, PREFS);
    
            final String[] header = beanReader.getHeader(true);
    
            // set up the field mapping, processors and hints dynamically
            final String[] fieldMapping = new String[header.length];
            final CellProcessor[] processors = new CellProcessor[header.length];
            final Class<?>[] hintTypes = new Class<?>[header.length];
            for (int i = 0; i < header.length; i++)
            {
                fieldMapping[i] = header[i];
                if (header[i].equals("firstname"))
                {
                    processors[i] = new NotNull();
                }
                if (header[i].equals("height") || header[i].equals("weight"))
                {
                    processors[i] = new ParseInt();
                }
            }
    
            beanReader.configureBeanMapping(Person.class, fieldMapping, hintTypes);
    
            Person person;
            while ((person = beanReader.read(Person.class, processors)) != null)
            {
                System.out.println(String.format("lineNo=%s, rowNo=%s, person=%s", beanReader.getLineNumber(),
                        beanReader.getRowNumber(), person));
            }
    
        }
        finally
        {
            if (beanReader != null)
            {
                beanReader.close();
            }
        }
    }