-1

copy one model class to another model class, class A and class B different properties name, want to set the class A properties to class B with manual mapping of field.

1 Answers1

0

ModelMapper provides multiple functionality, As per your requirement you can explore it out. Model Mapper Documentation Link

Giving a short example as below:

public class POJO {
    public static void main(String[] args) {
        ModelMapper mapper  = new ModelMapper();
        TypeMap<Source, Destiation> typemap = mapper.createTypeMap(Source.class,Destiation.class);
        typemap.addMappings(mappings -> mappings.map(Source::getName,Destiation::setNom));
        typemap.addMappings(mappings -> mappings.map(Source::getLastName,Destiation::setPrenom));

        Source source = new Source();
        source.setName("John");
        source.setLastName("Doe");
        Destiation destiation = mapper.map(source,Destiation.class);
        System.out.println(destiation.toString());
    }
}
@Data //lombok annotation
class Source{
    private String name;
    private String lastName;

}
@Data //lombok annotation
class Destiation{
    private String nom;
    private String prenom;
}
Rishal
  • 1,480
  • 1
  • 11
  • 19