Search code examples
javaorika

How to exclude field in Orika bean mapper?


I want to exclude some fields during mapping from a bean to HashMap.

Orika definition:

static {
        final MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(MyReq.class, Map.class)
                .exclude("myproperty")
                .byDefault()
                .register();
        MAPPER = mapperFactory.getMapperFacade();
    }

Bean definitions:

public class MyReq {
    private String myproperty;
}

Usage:

MyReq req = new MyReq;
Map map = MAPPER.map(req, Map.class);

Result: the Map contains the excluded myproperty field! Why?


Solution

  • I also faced this problem, but only with Map instances (it works OK when class that you defined is destination object). However, there is a workaround, since Orika has multiple ways to define mapping rules, something like this:

    mapperFactory.classMap(MyReq.class, Map.class)
                            .fieldMap("myproperty").exclude().add()
                            .byDefault()
                            .register();