简体   繁体   中英

Dozer nested class field to flat Map.class mapping using Dozer API

I'm trying to make Dozer map my classes:

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

public class B {
  private String someBText;
}

to a resulting Map.class like the following:

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

.

That is, I'm trying to specify nested class' field mapping to a flat Map key destination. I'm using Dozer by Java API, not xml. I wasn't able to find appropriate builder configuration to manage this. Base code is something like:

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));
  }
 }

I suggest you to try the following configuration:

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");
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM