5

I am trying to do a oneWay Mapping with Dozer from Source to Destination.

public class Source {
    Map<String, String> values;
    public Source() {
    }
    public Source(Map<String, String> values) {
        this.values = values;
    }
    public Map<String, String> getValues() {
        return values;
    }
    public void setValues(Map<String, String> values) {
        this.values = values;
    }
}

.

public class Destination {
    private String lastname;
    private String firstname;
    public Destination() {
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
}

Here is my Testclass:

public class DozerMapperTest {
    private DozerMapper mapper = new DozerMapper();
    @Test
    public void testName() throws Exception {
        String firstname = "Tom";
        String lastname = "Hanks";
        Map<String, String> input = new HashMap<>();
        input.put("firstname", firstname);
        input.put("lastname", lastname);
        Destination result = mapper.map(new Source(input));
        Assert.assertNotNull(result);
        Assert.assertEquals(firstname, result.getFirstname());
        Assert.assertEquals(lastname, result.getLastname());
    }
}

My Mapping Class looks like this:

public class DozerMapper {
    public DozerMapper() {
        initMapper();
    }
    private DozerBeanMapper mapper;
    public Destination map(final Source input) {
        return mapper.map(input, Destination.class);
    }
    void initMapper() {
        BeanMappingBuilder builder = new BeanMappingBuilder() {
            @Override
            protected void configure() {
                mapping(Source.class, Destination.class, TypeMappingOptions.oneWay())
                        .fields(new FieldDefinition("values.lastname"), "lastname")
                        .fields(new FieldDefinition("values.firstname"), "firstname");
            }
        };
        mapper = new DozerBeanMapper();
        mapper.addMapping(builder);
    }
}

But his is all not working :-( I also tried this mapping:

.fields(new FieldDefinition("values").mapKey("lastname"), "lastname")
.fields(new FieldDefinition("values").mapKey("firstname"), "firstname");

I googled, looked in the dokumentation and nothing. Can anybody help me or give me some hints?

Phil
  • 133
  • 5
  • Please include more information about the problem you are experiencing and what have you tried to fix that problem. Thanks! – Robert Rossmann Jan 02 '15 at 12:42
  • My problem is that I don't know how the mapping should me and I can not find any helpfull docu or examples. When I use this Mapping: fields(new FieldDefinition("values").mapKey("lastname"), "lastname"); Then the test fails becaúse the Destination.lastname contains "{lastname=Hanks, firstname=Tom}" I already did some debugging of the Dozer framework, but that was not really helpfull, because its not that easy. – Phil Jan 02 '15 at 14:39

1 Answers1

0

I myself didn't think this was possible until I researched it just now, so thanks for the question!

The correct way to do is this:

.fields("values", "firstname")
.fields("values", "lastname");

The key point here is that the variables "firstname" and "lastname" in Destination class has to be the same as the keys in the Source class' Map. Otherwise this won't work.

Ray
  • 3,864
  • 7
  • 24
  • 36