java - 使用 ModelMapper 映射抽象类型的字段
问题描述
我有以下类层次结构:
public abstract class Base {
protected Boolean baseBoolean;
}
public class A extends Base {
private BigDecimal amount;
}
并尝试将 DTO 映射到实体
public class DTO {
private Base details;
}
public class Entity {
private Base details;
}
并映射如下:
public static void main(String[] args) {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setDeepCopyEnabled(true);
A a = new A();
a.setAmount(BigDecimal.ONE);
a.setBaseBoolean(true);
DTO request = DTO.builder().base(a).build();
Entity entity = modelMapper.map(request, Entity.class);
System.out.println(entity);
}
我在详细信息字段中收到带有 A 或 B 的 DTO,这是使用调试器检查的。但是modelmapper抛出
无法实例化目标 org.package.Base 的实例。确保 org.package.Base 具有非私有的无参数构造函数。
我尝试使用显式提供程序(未用于此映射):
modelMapper.typeMap(A.class, Base.class).setProvider(new Provider<Base>() {
@Override
public Base get(ProvisionRequest<Base> r) {
return new A();
}
});
我也尝试实现这样的自定义转换器(也没有执行):
modelMapper.typeMap(A.class, Base.class).setConverter(new Converter<A, Base>() {
@Override
public Base convert(MappingContext<A, Base> mappingContext) {
return modelMapper.map(mappingContext.getSource(), A.class);
}
});
似乎模型映射器不将此类型映射用于字段,仅用于层次结构的根。在这种情况下如何映射类层次结构?
解决方案
如您所见,启用深层复制时会出现问题:modelMapper.getConfiguration().setDeepCopyEnabled(true)
.
一个解决方案是定义Converter<Base, Base>
如下:
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setDeepCopyEnabled(true); // this triggers the problem
// the converter resolves it
Converter<Base, Base> baseBaseConverter = context ->
modelMapper.map(context.getSource(), context.getSource().getClass());
modelMapper.createTypeMap(Base.class, Base.class).setConverter(baseBaseConverter);
这是有关该主题的更详细的帖子。
推荐阅读
- java - 如何取消当前循环并继续循环
- python - 访问地图函数时获取内存位置作为输出
- java - 尝试从相对路径读取文件时出现“java.io.IOException:流已关闭”
- php - DomPDF:在生产服务器中呈现时图像失真
- python - Django 找不到页面 [已回答]
- java - 线程“主”java.lang.NoClassDefFoundError 中的异常:解决方案(错误名称:解决方案)
- c# - 单击按钮时隐藏asp.net中的文本框
- sql - 如何编写一个 SQL WHERE 语句,其中包含应存在于一组列中的多个条目
- python - 如何使用 OneVsRestClassifier 和 NearestCentroid 分类器将多标签分类转换为二进制分类
- algorithm - 自适应梯形规则及优先队列实现的说明