what is the best way to implement a mapper in java?

14,022

Take a look at Dozer and modelmapper. They are both excellent tools for mapping one object to another. I know that Dozer integrates with Spring. In Dozer your code will look something like this:

Mapper mapper = new DozerBeanMapper();
ResponseTo response = mapper.map(request, ResponseTo.class);

It's very powerful and very easy to use.

Share:
14,022
mohanaki
Author by

mohanaki

Updated on June 04, 2022

Comments

  • mohanaki
    mohanaki about 2 years

    I have three request objects called Request1To,Request2To,Request3To. I want to have a method that takes in any of these TO's and return a response object. I have currently implemented like this

    class Mapper{
         public ResponseTo mapRequest1(Request1To){
    
          }
    
         public ResponseTo mapRequest2(Request2To){
    
          }
    
        public ResponseTo mapRequest3(Request3To){
    
          }
    
    }
    

    Is there a better way to do this instead of creating a seperate method for each request object?

  • mohanaki
    mohanaki over 11 years
    mapping for each request object is different.I need to have an if condition inside the mapRequest method to find what the instance type is and then do mapping which i dont like.
  • mohanaki
    mohanaki over 11 years
    i dont want to have chained if conditions.
  • someone
    someone over 11 years
    I changed my Answer check it
  • mohanaki
    mohanaki over 11 years
    I need to have the mapping logic in transferobject which i don't want to do.As having mapping logic in transfer object ties it to a particular response type.