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.

Share:
18,520

Related videos on Youtube

Harold L. Brown
Author by

Harold L. Brown

Updated on June 04, 2022

Comments

  • Harold L. Brown
    Harold L. Brown almost 2 years

    At Spring's @Cacheable annotation I want to specify an unless 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
      Sotirios Delimanolis about 10 years
      Can you be more specific? What are the two operands? What are you comparing?
    • Harold L. Brown
      Harold L. Brown about 10 years
      @SotiriosDelimanolis An instance of the Enum and a value of the enum. Like instance == Enum.CERTAIN_VALUE.
  • sorrymissjackson
    sorrymissjackson about 8 years
    You 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
    michaldo about 8 years
    Now is OK. I cancelled my comment
  • Wouter
    Wouter over 7 years
    This can be lot shorter than the accepted answer, because you do not need to write the whole fully qualified path.
  • aksh1618
    aksh1618 about 4 years
    But you'd have to ensure it always matches the enum variable in case it changes
  • NCrash
    NCrash over 2 years
    it works as well