chebad
chebad

Reputation: 1017

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?

Upvotes: 3

Views: 1263

Answers (1)

Sidi
Sidi

Reputation: 1739

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

Upvotes: 6

Related Questions