我正在尝试实现一些简单的补丁风格的功能,这将允许我采取任意对象并将类似命名的字段映射到我的对象。如果我不想配置任何东西,这很有用,但是如果我想要排除某些字段,Dozer只映射我指定的字段(实际上,通配符设置为false)。有没有办法完成我正在尝试的东西?
以下是代码中的问题:
public class Update {
String a;
String b;
// constructor, getters, and setters omitted
}
public class Destination {
String a;
String b;
String c;
// constructor, getters, and setters omitted
}
我想阻止人们指定b。使用dozer的java配置:
BeanMappingBuilder builder = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(Object.class, Destination.class,
TypeMappingOptions.oneWay(),
TypeMappingOptions.wildcard(true),
TypeMappingOptions.mapId("Any-To-Destinaion"))
.exclude("b");
}
};
final DozerBeanMapper mapper = new DozerBeanMapper();
mapper.addMapping(builder);
所以我应该可以这样称呼:
Update u = new Update("uA", "uB");
Destination d = new Destination("dA", "dB", "dC");
mapper.map(u, d, "Any-To-Destination");
// these should all print true
System.out.println("a: " + d.getA().equals("uA"));
System.out.println("b: " + d.getB().equals("dB"));
System.out.println("c: " + d.getB().equals("dC"));
输出是:
a: false
b: true
c: true
无论如何要做到这一点?