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

chebad :

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?

Sidi :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88303&siteId=1