Search code examples
javaorika

Orika map two String fields into one - taking into account the remaining fields


I have situacion like this:

class Person {
    String firstName;
    String lastName;
    Integer age;
    Float height;
//seters, getters, etc.
}

class PersonDto{
     String name; // it should be: firstName + " " + lastName
     Integer personAge;
     Float height;
}

How can I map Person --> PersonDto with all fields?


Solution

  • You can use :

    mapperFactory.classMap(Person.class, PersonDTO.class)
    .field("age","personAge")
    .byDefault()
    .customize(
       new CustomMapper<Person, PersonDTO> {
          public void mapAtoB(Person a, PersonDTO b, MappingContext context) {
             b.setName(a.getFirstName()+ " "+a.getLastName());
          } 
       })
    .register();