Java ModelMapper在不同的属性上混淆
我有一个Java ModelMapper在不同的属性上混淆,java,modelmapper,Java,Modelmapper,我有一个Student对象扩展Person对象 public abstract class Person implements IIdentifiable { private String contactNumber; // other properties public String getContactNumber() { return contactNumber; } public void setContactNumber(Str
Student
对象扩展Person
对象
public abstract class Person implements IIdentifiable {
private String contactNumber;
// other properties
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
}
public class Student extends Person {
private String studentNumber;
//Other properties
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
}
学生有一个属性studentNumber
,而个人有一个属性contactNumber
。当我将Student对象映射到StudentDto
时,它会对给定的属性感到困惑
public class StudentDto{
private String studentNumber;
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
}
这只发生在某些场合。我想知道原因是什么
1) The destination property com.cinglevue.veip.web.dto.timetable.StudentDto.setStudentNumber() matches multiple source property hierarchies:
com.cinglevue.veip.domain.core.student.StudentProfile.getStudent()/com.cinglevue.veip.domain.core.Person.getContactNumber()
com.cinglevue.veip.domain.core.student.StudentProfile.getStudent()/com.cinglevue.veip.domain.core.Student.getStudentNumber()
你已经很久没问了。因为这里没有答案,所以我向您展示我的解决方案,它对我来说非常有效 这个问题是由目标属性名称引起的,让ModelMapper感到困惑。 所以,要解决这个问题,我们需要做两个步骤。 1.假设ModelMapper忽略了一些可能令人困惑的内容。 2.为混乱的属性指定指示映射 详细代码如下:
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setAmbiguityIgnored(true);
modelMapper.createTypeMap(Student.class, StudentDto.class)
.addMapping(Student::getStudentNumber, StudentDto::setStudentNumber);
1.
您可以使用以下方法更改匹配策略
:
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
PS:modelmapper隐式地使用匹配策略.STANDARD
但它要求源端和目标端的属性名令牌精确匹配
2.
告诉ModelMapper在找到多个源属性层次结构时忽略映射:
modelMapper.getConfiguration().setAmbiguityIgnored(true);
你可以发布你的试用代码,以及学生和个人类确认。我更新了post您可以发布您的ModelMapper配置和映射吗?或者只是发生错误时的代码用例图。是的,我记不起为什么我现在有这个问题。无论如何,有人可能会受益。谢谢,我不记得为什么我现在有这个问题了。无论如何,有人可能会受益。谢谢