Skip to content
Advertisement

How to map multiple enums to string (and back) using their common property with MapStruct?

I have two enums with common property – id:

JavaScript

And I need generic MapStruct mapping of such enums (without need to define mapping of every such enum).

Advertisement

Answer

Yes, it’s possible through @TargetType annotation. You need a common interface for such enums:

JavaScript

Then you need to be able to create enum of such interface instance by id:

JavaScript

Then the generic Mapper may look like this (@TargetType annotation is the key to success):

JavaScript

Just use it your mapper that maps a class that contains such enums:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement