【发布时间】:2020-12-30 05:42:28
【问题描述】:
第 1 部分
我正在使用 Java ModelMapper 库 (http://modelmapper.org/) 来管理我的实体和 DTO 之间的映射。我有一个联系人(实体)和一个联系人视图(DTO)。 我在 ContactView 中有一个在 Contact 中不存在的字符串字段,称为“type”。 它的值应该只是实体子类的名称。 我试图像这样制作这个自定义映射:
modelMapper.typeMap(Contact.class, ContactView.class).addMappings(mapper -> {
mapper.map(src -> src.getClass().getSimpleName(), ContactView::setType);
});
我在以下位置收到编译错误: mapper.map(src -> src.getClass().getSimpleName(), ContactView::setType);
定义了非法的 SourceGetter
1 处错误 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.2.jar:5.3.2] 在 org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.2.jar:5.3.2] ...省略了33个常用框架
我什至尝试使用转换器,结果相同:
modelMapper.typeMap(Contact.class, ContactView.class).addMappings(mapper -> {
Converter<Class, String> toName = ctx -> ctx.getSource() == null ? null : ctx.getSource().getSimpleName();
mapper.using(toName).map(Contact::getClass, ContactView::setType);
});
你知道如何解决这个问题吗?
第 2 部分
按照建议的答案,我尝试向 ModelMapper 添加一个转换器类。这是我配置 ModelMapper Bean 的地方:
@Configuration
public class Mapper {
@Autowired
private ContactTypeRepository contactTypeRepository;
@Bean
public ModelMapper getMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT);
modelMapper.typeMap(ContactTag.class, ReferenceEntityView.class).addMappings(mapper -> {
mapper.map(src -> src.getTag().getCode(), ReferenceEntityView::setCode);
mapper.map(src -> src.getTag().getValue(), ReferenceEntityView::setValue);
});
modelMapper.typeMap(Person.class, PersonView.class).addMappings(mapper -> {
mapper.skip(PersonView::setName);
mapper.map(Person::getName, PersonView::setLastName);
});
modelMapper.addConverter(new ContactConverter());
return modelMapper;
}
class ContactConverter implements Converter<Contact, ContactView> {
private ModelMapper localMapper = new ModelMapper();
@Override
public ContactView convert(MappingContext<Contact, ContactView> context) {
Contact contact = context.getSource();
ContactView contactView = localMapper.map(contact, ContactView.class);
ContactType contactType = contactTypeRepository.getByCode(context.getSource().getClass().getSimpleName().toLowerCase());
contactView.setType(localMapper.map(contactType, ReferenceEntityView.class));
return contactView;
}
}
}
这是我使用 ModelMapper Bean 生成 DTO 的地方:
@RestController
@RequestMapping(value = "/contacts")
public class ContactController {
@Autowired
private ContactRepository contactRepository;
@Autowired
private ModelMapper modelMapper;
@GetMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK)
public ContactView findById(@PathVariable("id") Long id){
Contact c = contactRepository.getOne(id);
ContactView cv = modelMapper.map(c, ContactView.class);
return cv;
}
}
由于某种原因,未调用 Converter 的 convert 方法,并且 ContactView 对象的“type”字段为空。 ModelMapper Bean 上的其他映射工作正常。
【问题讨论】:
标签: java spring modelmapper