Sebastien
Sebastien

Reputation: 13

Spring expression (SpEL) - Elvis operator : cannot convert from Integer to Boolean error

I am using Spring expression version 4.3.2.RELEASE It seems we cannot use the Elvis operator for any other types than String and Boolean.

For example, the following will thrown an error: field ?: 2 > 0

Can we work around this? is it a defect in SpEL?

thanks,

Sebastien

Upvotes: 1

Views: 2594

Answers (2)

eltabo
eltabo

Reputation: 3807

It seems an operator precedence issue. Maybe your expression should be (field ?: 2)>0

Hope it helps.

Upvotes: 0

gschambial
gschambial

Reputation: 1391

Elvis operator is shorthand notation for ternary operator, used in case of nullability check.

Its syntax is:

someField?:somevalue

where, someField can be of any type. Above expression will return value of someField (e.g. Integer), if it's not null else it will return someValue. someValue must be of same type as someField (Integer).

So, This is not a limitation of SPel. It is the specific usage of operator.

In your example, field is an integer so, resolved value must also be of integer type. But, you are doing 2>0 that resolves to boolean type, which is not valid in this case.

What you can do is (field?: 2) > 0, if it is what you are trying to achieve.

I hope, it clarifies.

Upvotes: 2

Related Questions