使用 modelmapper 映射字段时如何应用自定义逻辑
How to apply custom logic when mapping fields using modelmapper
如何将自定义逻辑应用于字段映射?在此示例中,我想从 LocalDate birthdate
.
中计算出 int age
public class ModelMapperConfigTest {
@Data
public static class Person {
private LocalDate birthdate;
}
@Data
public static class PersonDto {
private long age;
}
// Test data
LocalDate birthdate = LocalDate.of(1985, 10, 5);
long age = 32;
Clock clock = Clock.fixed(birthdate.plusYears(age).atStartOfDay(ZoneId.systemDefault()).toInstant(), ZoneId.systemDefault());
@Test
public void should_calc_age() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
// TODO: How to configure the Mapper so that calcBirthdate is used?
// test that all fields are mapped
modelMapper.validate();
// test that the age is calculated
Person person = new Person();
person.setBirthdate(birthdate);
PersonDto personDto = modelMapper.map(person, PersonDto.class);
assertTrue(personDto.age == age);
}
// This method should be used for mapping. In real, this could be a service call
private long calcBirthdate(LocalDate birthdate) {
return YEARS.between(birthdate, LocalDate.now(clock));
}
}
您可以为此使用 Converter
s。在您的情况下,注册一个 Converter<LocalDate, Long>
用于从 birthdate
到 age
的映射。
这可以用这样的 lambda 来完成:
@Test
public void should_calc_age() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
modelMapper.createTypeMap(Person.class, PersonDto.class)
.addMappings(mapper ->
mapper
// This is your converter.
// Here you have access to getSource() (birthdate).
.using(ctx -> calcBirthdate((LocalDate) ctx.getSource()))
// Now define the mapping birthdate to age
.map(Person::getBirthdate, PersonDto::setAge));
// test that all fields are mapped
modelMapper.validate();
// test that the age is calculated
Person person = new Person();
person.setBirthdate(birthdate);
PersonDto personDto = modelMapper.map(person, PersonDto.class);
assertTrue(personDto.age == age);
}
如何将自定义逻辑应用于字段映射?在此示例中,我想从 LocalDate birthdate
.
int age
public class ModelMapperConfigTest {
@Data
public static class Person {
private LocalDate birthdate;
}
@Data
public static class PersonDto {
private long age;
}
// Test data
LocalDate birthdate = LocalDate.of(1985, 10, 5);
long age = 32;
Clock clock = Clock.fixed(birthdate.plusYears(age).atStartOfDay(ZoneId.systemDefault()).toInstant(), ZoneId.systemDefault());
@Test
public void should_calc_age() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
// TODO: How to configure the Mapper so that calcBirthdate is used?
// test that all fields are mapped
modelMapper.validate();
// test that the age is calculated
Person person = new Person();
person.setBirthdate(birthdate);
PersonDto personDto = modelMapper.map(person, PersonDto.class);
assertTrue(personDto.age == age);
}
// This method should be used for mapping. In real, this could be a service call
private long calcBirthdate(LocalDate birthdate) {
return YEARS.between(birthdate, LocalDate.now(clock));
}
}
您可以为此使用 Converter
s。在您的情况下,注册一个 Converter<LocalDate, Long>
用于从 birthdate
到 age
的映射。
这可以用这样的 lambda 来完成:
@Test
public void should_calc_age() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
modelMapper.createTypeMap(Person.class, PersonDto.class)
.addMappings(mapper ->
mapper
// This is your converter.
// Here you have access to getSource() (birthdate).
.using(ctx -> calcBirthdate((LocalDate) ctx.getSource()))
// Now define the mapping birthdate to age
.map(Person::getBirthdate, PersonDto::setAge));
// test that all fields are mapped
modelMapper.validate();
// test that the age is calculated
Person person = new Person();
person.setBirthdate(birthdate);
PersonDto personDto = modelMapper.map(person, PersonDto.class);
assertTrue(personDto.age == age);
}