Dozer使用Dozer API嵌套类字段以平面Map.class映射

时间:2015-05-27 11:32:15

标签: java dozer

我试图让Dozer映射我的课程:

public class A {
  private B someB;
  private String someAText;
}

public class B {
  private String someBText;
}

生成Map.class,如下所示:

"someAText" => "someATextValue"
"someBText" => "someBTextValue"

也就是说,我试图指定嵌套类'字段映射到平面Map密钥目标。我使用的是Dozer by Java API,而不是xml。我无法找到合适的构建器配置来管理它。基本代码如下:

beanMappingBuilder = new BeanMappingBuilder() {
  @Override
  protected void configure() {
    mapping(B.class, Map.class, TypeMappingOptions.oneWay(), mapNull(true));
    mapping(A.class, Map.class, TypeMappingOptions.oneWay(), mapNull(true));
  }
 }

1 个答案:

答案 0 :(得分:0)

我建议您尝试以下配置:

beanMappingBuilder = new BeanMappingBuilder() {

  @Override
  protected void configure() {
    // 'A > Map' mapping
    mapping(A.class, Map.class, TypeMappingOptions.oneWay(), TypeMappingOptions.mapNull(true))
      .fields("someAText", "someATextValue")
      .fields("someB.someBText", "someBTextValue");

    // 'B > Map' mapping
    mapping(B.class, Map.class, TypeMappingOptions.oneWay(), TypeMappingOptions.mapNull(true))
      .fields("someBText", "someBTextValue");
  }
}