Display dispenser from an object using Map to Pojo with properties

I am trying to make a one-time comparison with a bulldozer 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 test class:

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 is as follows:

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 its still not working :-( I also tried this mapping:

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

I googled, looked at the documentation and nothing. Can someone help me or give me some advice?

+4
source share
1 answer

I myself did not think it was possible until I examined it now, so thanks for the question!

The correct way to do this is:

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

, "firstname" "lastname" Destination , " ". .

0

Source: https://habr.com/ru/post/1569698/


All Articles