Home > Blockchain >  Spring Boot Spel expression concatenate variables and convert them to lower case?
Spring Boot Spel expression concatenate variables and convert them to lower case?

Time:06-14

Consider this method signature that I have

@Cacheable(
  key="#name '::' #age '::' #country"
)
public User validate(String name, String age, String country) {
...
}

Notice the key that I have. Currently I am able to concatenate the name, age and country variables. But on top of concatenating them, I also want to convert the entire expression into a lowercase value.

For example if someone called this method like so: validate("John","24","USA"),

then I want my key to be resolved as: john::24::usa. Notice all the uppercase letters have become lowercase.

TLDR; how to write a spel expression which concatenates multiple variables and converts the entire result into lowercase?

CodePudding user response:

Expression exp = new SpelExpressionParser()
    .parseExpression("(#foo   '::'   #bar).toLowerCase()");
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("foo", "XXX");
ctx.setVariable("bar", "YYY");
System.out.println(exp.getValue(ctx));
xxx::yyy
  • Related