数据脱敏

属性拷贝过程中,可能会有数据脱敏或者格式化的需求。对此,@Mapping注解提供了mask属性,可以灵活配置字符的模板。

class Foo {
    String email = "test@taocares.com";
    String mobile = "13812345678";
}
class FooDto {
    @Mapping(mask="AAA<@")
    String email;
    @Mapping(field="mobile", mask="AAA****AAAA")
    String mobile1;
    @Mapping(field="mobile", mask="AAA' 'AAAA' 'AAAA")
    String mobile2;
}

FooDto fooDto = BeanUtils.copyProperties(foo, FooDto.class);

System.out.println(fooDto.getEmail()); // "tes*@taocares.com"
System.out.println(fooDto.getMobile1()); // "138****5678"
System.out.println(fooDto.getMobile2()); // "138 1234 5678"

Last updated