使用 super-CSV 读取嵌套的 POJO
Reading nested POJO with super-CSV
我试图用 CSV 加载嵌套的 POJO。由于某些错误,我无法使其工作。
请在下面找到我的代码。下面的class Person包含两个POJO字段address和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
}
地址class
public class Address
{
public String city;
public String zipCode;
public GeoLocation geoLocation;
//getter和setter
}
地理位置
public class GeoLocation
{
private String latitude;
private String longitude;
//getter和setter
}
现在我正在尝试读取 CSV 并填充人员 class。我无法让它工作。这是我的代码
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();
}
}
}
}
我收到以下错误
线程 "main" org.dozer.MappingException 中的异常:class
中未找到字段(高度)的读取或写入方法
我让它工作了。问题出在字段映射中。
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();
}
}
}
我试图用 CSV 加载嵌套的 POJO。由于某些错误,我无法使其工作。
请在下面找到我的代码。下面的class Person包含两个POJO字段address和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
}
地址class
public class Address
{
public String city;
public String zipCode;
public GeoLocation geoLocation;
//getter和setter }
地理位置
public class GeoLocation
{
private String latitude;
private String longitude;
//getter和setter }
现在我正在尝试读取 CSV 并填充人员 class。我无法让它工作。这是我的代码
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();
}
}
}
}
我收到以下错误 线程 "main" org.dozer.MappingException 中的异常:class
中未找到字段(高度)的读取或写入方法我让它工作了。问题出在字段映射中。
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();
}
}
}