我有一个包含以下列的CSV文件:
Item_1 12 2
Item_2 10 1
Item_3 15 6
我想阅读并创建模型。
final String[] fieldMapping = getFieldMapping().toArray(new String[fileParsingInput.getFieldMapping().size()]);
try (final CsvDozerBeanReader csvDozerBeanReader = new CsvDozerBeanReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8), CsvPreference.STANDARD_PREFERENCE)) {
csvDozerBeanReader.configureBeanMapping(BillingModel.class, fieldMapping);
T rowObject;
while ((rowObject = csvDozerBeanReader.read(BillingModel.class, cellProcessors)) != null) {
rowObjects.add(rowObject);
}
}
private List<String> getFieldMapping() {
final List<String> fieldMappings = new ArrayList<>();
fieldMappings.add("itemDetail.itemId");
fieldMappings.add("itemDetail.cost");
fieldMappings.add("itemDetail.quantity");
return fieldMappings;
}
我的模型类是:
public abstract class ItemDetail {
}
@Getter
@Setter
@NoArgsConstructor
public class ProductDetail extends ItemDetail {
private String itemId;
private Long cost;
private Long quantity;
public ProductDetail() {
this.itemId = itemId;
this.cost = cost;
this.quantity = quantity;
}
}
@Getter
@Setter
public class BillingModel {
private ItemDetail itemDetail;
private String clientName;
}
我遇到一个例外-
org.dozer.MappingException:在类(BillingModel)类中没有找到对字段(itemDetail.itemId)的读取或写入方法。
我发现问题在于,因为ItemDetail是抽象的,所以推土机无法确定要实例化的类。我可以这样做吗?如果可以,有人可以帮助我吗?有没有一种方法可以基于映射在运行时实例化适当的类?