Not able to map String to Integer via Enum

15 views
Skip to first unread message

Jagadeesh Sadashiv

unread,
Dec 12, 2018, 6:05:21 PM12/12/18
to modelmapper
Source Object

Request {
    public String classification;
}

Destination Object

DTO {
    public Long classificationId;
}

There is an ENUM that maps classification text to its Id.

Classification {
    POWER(3, "Power")

    private final Integer code;
private final String name;

    ProblemReproducible(final int code, final String name) {
this.code = code;
this.name = name;
}

    getCodeByName(context.getSource()){
       Optional< Classification > classification = Arrays.asList(Classification.values()).stream()
        .filter(item -> item.getName().equalsIgnoreCase(name))
.findFirst();
return classification.isPresent() ? classification.get().getCode() : null;
    }
}


ModelMapper is not mapping values. I have added converted like below.

Converter<String, Integer> converter = context -> (context.getSource() == null) ? null : Classification.getCodeByName(context.getSource());

Reply all
Reply to author
Forward
0 new messages