spring-el

SPeL case insensitive evaluation


I am using Spring expression language programmatically to evaluate certain conditions against an object

Scenario

Car car= new Car()
car.setMake("tesla");
car.setOwner(""abc);

String expresionString="( (make == 'TESLA') && owner =='ABC' )"


ExpressionParser expressionParser = new SpelExpressionParser();
Expression expression=expressionParser.parseExpression(expresionString);
Boolean result=expression.getValue(car,Boolean.class);

System.out.println("matching? "+result);

The result is false because of the case mismatch

Is there anyway to achieve case insensitive evaluation using SpeL. Also is it possible to check contains operation similar to String.contains


Solution

  • You can call to upper case on the attributes:

    String expresionString="( make.toUpperCase == 'TESLA' && owner.toUpperCase =='ABC' )"