如何使用 DozerBeanMapper 将具有对象数组列表的 HashMap(列表是哈希映射中的值)映射到另一个 HashMap?

How to map HashMap with Array List of Objects (list is the value in the hash map) into another HashMap using DozerBeanMapper?

我有两个 class,我想使用 org.dozer.Mapper(http://dozer.sourceforge.net/).

将 Female 对象的属性映射到 Male 对象

第一个class是:

public class Male {
    private String name;
    private String surname;

    private Map<String, List<Contact>> contacts;
....

第二个class是:

public class Female {
    private String name;
    private String surname;
    private String mobile;
    private String dateOfBirth;

    private Map<String, List<Contact>> contacts;
...

第三个class是:

public class Contact {
    private String street;
    private String postcode;
    private String email;
...

我像对象属性一样使用的地图是LinkedHashMap,地图中的值列表是ArrayList。 当我尝试使用推土机映射它们时,作为散列映射中值的数组列表不是包含对象的列表,如图所示:

        Map<String, List<Contact>> contact = new LinkedHashMap<>();
        List<Contact> listOfContacts = new ArrayList<>();
        Contact contactObj = new Contact();
        contactObj.setEmail("lala@gmail.com");
        contactObj.setPostcode("1233355");
        contactObj.setStreet("street");

        listOfContacts.add(contactObj);

        contact.put("2131323213", listOfContacts);
        femaleObj.setContact(contact);

        Mapper objectMapper = new DozerBeanMapper();
        Male maleObj = objectMapper.map(femaleObj, Male.class);

如何获取Male对象中List中的对象列表?

起初,我试过你的代码as-is,我得到了同样的行为。

然后,我已经使用 b-hint 显式设置了映射配置(参见 documentation about this),如下所示,我得到了您需要的。

第一种情况——Java配置(创建一个扩展BeanMappingBuilder的class):

public class CustomMapper extends BeanMappingBuilder {
    @Override
    protected void configure() {
        mapping(Female.class, Male.class).fields("contacts", "contacts", FieldsMappingOptions.hintB(Contact.class));
    }
}

第二种情况-XML 配置:

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">

  <configuration>
    <stop-on-errors>true</stop-on-errors>
    <wildcard>true</wildcard>
  </configuration>

  <mapping>
    <class-a>blog.valerioemanuele.dozer.Female</class-a>
    <class-b>blog.valerioemanuele.dozer.Male</class-b>

      <field>
        <a>contacts</a>
        <b>contacts</b>
        <b-hint>blog.valerioemanuele.dozer.Contact</b-hint> 
      </field>
  </mapping> 


</mappings>

这里是我执行的单元测试:

import org.dozer.DozerBeanMapper;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class TestFemaleToMaleConversion {

    private static Female femaleObj;

    @BeforeAll
    private static void initData() {
        Map<String, List<Contact>> contact = new LinkedHashMap<>();
        List<Contact> listOfContacts = new ArrayList<>();
        Contact contactObj = new Contact();
        contactObj.setEmail("lala@gmail.com");
        contactObj.setPostcode("1233355");
        contactObj.setStreet("street");

        listOfContacts.add(contactObj);

        contact.put("2131323213", listOfContacts);
        femaleObj = new Female();
        femaleObj.setName("Elisabeth");
        femaleObj.setSurname("Chesny");
        femaleObj.setContacts(contact);
    }

    @Test
    void testWithXmlMapping() {
        DozerBeanMapper objectMapper = new DozerBeanMapper();
        objectMapper.setMappingFiles(Arrays.asList("dozer-mapping.xml"));
        Male maleObj = objectMapper.map(femaleObj, Male.class);

        Assert.assertEquals("lala@gmail.com", maleObj.getContacts().get("2131323213").get(0).getEmail());
    }

    @Test
    void testWithJavaMapping() {
        DozerBeanMapper objectMapper = new DozerBeanMapper();
        objectMapper.addMapping(new CustomMapper());
        Male maleObj = objectMapper.map(femaleObj, Male.class);

        Assert.assertEquals("street", maleObj.getContacts().get("2131323213").get(0).getStreet());
    }
}

这里是结果:


您可以从my GitHub repository获得完整的代码。该示例是使用 Java8、Maven 和 Junit5 开发的。

编辑:我添加了 Java 映射配置案例。从 another post

中汲取灵感

如果您想使用 JAVA 代码而不是 XML 配置实现相同的效果,请使用此:

public class DemoProvider extends BeanMappingBuilder {

    @Override
    protected void configure() {
        mapping(Female.class, Male.class,
                TypeMappingOptions.oneWay()
        )
                .fields("contact", "contact",
                        FieldsMappingOptions.collectionStrategy(true, RelationshipType.NON_CUMULATIVE),
                        FieldsMappingOptions.hintA(Contact.class),
                        FieldsMappingOptions.hintB(Contact.class),
                        FieldsMappingOptions.oneWay()
                );
      }

}

用法:

DemoProvider demoProvider = new DemoProvider();
DozerBeanMapper objectMapper = new DozerBeanMapper();
objectMapper.addMapping(demoProvider);
Male maleObj = objectMapper.map(femaleObj, Male.class);