Compare Enums in SpEL
18,520
Solution 1
#result == T(fully.qualified.path.to.AnEnum).A_VALUE
Solution 2
#object.someEnumProperty.name() == 'CERTAIN_VALUE'
would work as well.
Related videos on Youtube
Author by
Harold L. Brown
Updated on June 04, 2022Comments
-
Harold L. Brown almost 2 years
At Spring's
@Cacheable
annotation I want to specify anunless
condition.However my return value is neither a primitive type nor a Java bean, but an Enum.
How can I compare for equality with another Enum in SpEL (Spring Expression Language)?
-
Sotirios Delimanolis about 10 yearsCan you be more specific? What are the two operands? What are you comparing?
-
Harold L. Brown about 10 years@SotiriosDelimanolis An instance of the Enum and a value of the enum. Like
instance == Enum.CERTAIN_VALUE
.
-
-
sorrymissjackson about 8 yearsYou are right @michaldo. I changed my answer to something that actually works. Wrote this from the top of my head. Sorry for the first bad attempt.
-
michaldo about 8 yearsNow is OK. I cancelled my comment
-
Wouter over 7 yearsThis can be lot shorter than the accepted answer, because you do not need to write the whole fully qualified path.
-
aksh1618 about 4 yearsBut you'd have to ensure it always matches the enum variable in case it changes
-
NCrash over 2 yearsit works as well