javaspringspring-mvcspring-el

How to handle "Property or field cannot be found on object in SpEL"?


How to handle Property or field <foo> cannot be found on object of type <bar> in SpEL ? e.g. If data doesn't have placeId property on it then It shouldn't give me above error and return differentValue.

"${@block?.value?.data?.placeId ?: 'differentValue'}"


Solution

  • That's not correct. SpEL is just an another JVM language. It's not non-typed language like JavaScript. So, what is going to happen if your Java class doesn't have some property? Right: Property or field <foo> cannot be found on object of type <bar>.

    Therefore a logic in the expression must not rely on some non-Java reflection logic.

    You definitely need to have there something like instanceof, but you may live without casting to get access to existing properties.