Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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"),

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading