ModelMapper:将规则应用于所有类型的字段
ModelMapper: apply rule to all fields of type
我有一个包含很多字符串字段(有些是其他类型)的 DTO。
将对象映射到另一个类似 class 的对象时,我需要将转换应用于 String 类型的所有字段,在这种情况下 - trim()
.
所以,我有:
class MyDTO {
String name;
String type;
String company;
String status;
String whatever;
... (there are about 80 String fields)
int someNumber;
double otherNumber;
}
class DestinationDTO { // has just some fields of MyDTO
String name;
String type;
String company;
String whatever;
int somenumber;
}
我尝试过的:
Converter<String, String> myConverter = c -> c.getSource().trim();
ModelMapper mm = new ModelMapper();
...
mm.typeMap(MyDTO.class, DestinationDTO.class)
.addMappings(c -> c
.using(myConverter)
.map(MyDTO::getName, DestinationDTO::getName)
) // this applies to just one field, I need all String fields
有没有办法一次指定 class 的所有字符串字段,而不必列出它们?
尝试搜索 modelmapper.org 文档,但我看到的只是一个接一个地配置字段。
有什么想法吗?
您可以使用 converters to specify transformations from one type to another. The source and target type can be the same. Use the addConverter<S,D>()
方法将通用转换器添加到 ModelMapper 本身(而不是单独添加到特定字段)。你可以这样使用它:
ModelMapper mm = new ModelMapper();
Converter<String, String> myConverter = new AbstractConverter<String, String>() {
protected String convert(String source) {
return source == null ? null : source.trim();
}
};
mm.addConverter(myConverter);
mm.getConfiguration().setFieldMatchingEnabled(true);
MyDTO source = new MyDTO();
source.name = " abc \t";
source.company = "\nd e f \n";
DestinationDTO target = mm.map(source, DestinationDTO.class);
System.out.printf("Target.name: '%s'%n", target.name);
System.out.printf("Target.company: '%s'%n", target.company);
输出将符合预期:
Target.name: 'abc'
Target.company: 'd e f'
我有一个包含很多字符串字段(有些是其他类型)的 DTO。
将对象映射到另一个类似 class 的对象时,我需要将转换应用于 String 类型的所有字段,在这种情况下 - trim()
.
所以,我有:
class MyDTO {
String name;
String type;
String company;
String status;
String whatever;
... (there are about 80 String fields)
int someNumber;
double otherNumber;
}
class DestinationDTO { // has just some fields of MyDTO
String name;
String type;
String company;
String whatever;
int somenumber;
}
我尝试过的:
Converter<String, String> myConverter = c -> c.getSource().trim();
ModelMapper mm = new ModelMapper();
...
mm.typeMap(MyDTO.class, DestinationDTO.class)
.addMappings(c -> c
.using(myConverter)
.map(MyDTO::getName, DestinationDTO::getName)
) // this applies to just one field, I need all String fields
有没有办法一次指定 class 的所有字符串字段,而不必列出它们?
尝试搜索 modelmapper.org 文档,但我看到的只是一个接一个地配置字段。
有什么想法吗?
您可以使用 converters to specify transformations from one type to another. The source and target type can be the same. Use the addConverter<S,D>()
方法将通用转换器添加到 ModelMapper 本身(而不是单独添加到特定字段)。你可以这样使用它:
ModelMapper mm = new ModelMapper();
Converter<String, String> myConverter = new AbstractConverter<String, String>() {
protected String convert(String source) {
return source == null ? null : source.trim();
}
};
mm.addConverter(myConverter);
mm.getConfiguration().setFieldMatchingEnabled(true);
MyDTO source = new MyDTO();
source.name = " abc \t";
source.company = "\nd e f \n";
DestinationDTO target = mm.map(source, DestinationDTO.class);
System.out.printf("Target.name: '%s'%n", target.name);
System.out.printf("Target.company: '%s'%n", target.company);
输出将符合预期:
Target.name: 'abc'
Target.company: 'd e f'