Search code examples
javaspring-bootjava-8orika

How to exclude object property


I using Orika mapper to map two beans. i would like to exclude billingSummary.billableItems property while mapping. I am trying below option but it is not working.

Any help?

public class Cart {
       private String id;
       private String name;
       private BillingSummary billingSummary;
       private String address;
       //with getter and setter methods
    }

   public class BillingSummary {
       private String billingItem;
       private String billingItemId;
       private BillableItems billableItems;
       ...
       // with getter setter methods
   }

   //FilteredCart is same as Cart.


 public class FilteredCart { 
        private String id;
        private String name;
        private BillingSummary billingSummary;
        private String address;
        //with getter and setter methods
    } 

@Component   
public class CartMapper extends ConfigurableMapper {
        @Override
        public void configure(MapperFactory mapperFactory) {
        mapperFactory.classMap(Cart.class,FilteredCart.class).exclude("billingSummary.billableItems").byDefault().register();
        }
}

Solution

  • What you can do is adding another mapping to the mapperFactory in order to define how you want to map the BillingSummary to itself. In this way, when mapping from Cart to FilteredCart, you can configure to exclude to map the billableItems.

    Therefore, your CartMapper will look like this:

    @Component   
    public class CartMapper extends ConfigurableMapper {
    
        @Override
        public void configure(MapperFactory mapperFactory) {
            mapperFactory.classMap(BillingSummary.class, BillingSummary.class).exclude("billableItems").byDefault().register();
            mapperFactory.classMap(Cart.class,FilteredCart.class).byDefault().register();
        }
    }