简体   繁体   中英

unable to generate mapping method from Enum To Boolean by MapStruct

I can use succesfully from enum to enum by using mapstruct which converts one object type to another object type.

Unfortunately, I am not able to convert Enum to Boolaen. I get following error.

"Can't map property "ChoiceType isPriceHigh" to "java.lang.Boolean isPriceHigh". Consider to declare/implement a mapping method: "java.lang.Boolean map(ChoiceType value)".

Here is the method that I use at mapstruct. Any help will be appreciated.

Cheers Alper

@ValueMappings({
        @ValueMapping(target = "true", source = "YES"),
        @ValueMapping(target = "false", source = "NO")
})
Boolean map(ChoiceType value);

@ValueMappings can be used to map between enum (s) and not object and enum . Boolean in Java is not an enum and that is the reason why you are getting the error.

In order to do such mapping you will have to define your own method for it.

public interface MyMapper {

    default Boolean map(ChoiceType value) {
        if (value == null) {
            return null;
        }

        switch(value) {
            case YES:
                return true;
            case NO:
            default:
                return false;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM