Question

I have two fields that I want to map using Orika

 @Override
    public void configure(MapperFactory factory) {
        factory.classMap(ClazzA.class, ClazzB.class)
                .byDefault()
                .field("name", "sname")
                .register();

I want to map that field name to the value from sname only if a third field "type" in ClazzB matches a string "stk".

Is it possible using Orika?

Était-ce utile?

La solution

Yes it is possible. Simply add a custom mapper after regular field mapping.

    factory.classMap(ClazzA.class, ClazzB.class)
                .field("name", "sname")
                .byDefault()
                .customize(new CustomMapper<ClazzA, ClazzB>() {
                    @Override
                    public void mapBtoA(ClazzB clazzB, ClazzA clazzA, MappingContext context) {
                        if ("stk".equals(clazzB.getType())) {
                            clazzA.setName(clazzB.getSName());
                        }
                    }                   
                }
                .register(); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top