Search code examples
javaspringannotationsspring-el

SpEL function changes String


I am trying to take a String from properties, run an operation on it, then store it as a variable with the @Value annotation. Unfortunately, when I use the #{'${variable}'} syntax that I need to use to use SpEL, the String changes. If I have two double quotes (""), they are changed to a single double quote (") which prevents me from deserializing the String. How do I prevent Spring from removing that 2nd quotation mark?

myProperties.properties
myValue={"myWorkingKey": "Hi!", "myNonWorkingKey": ""}


MyClass.java
public class MyClass {

  @Value("#{'${myValue}'}")
  public void printWithSpEL(String withSpEL){
    System.out.println(withSpEL);
  }

  @Value("${myValue}")
  public void printWithOut(String without){
    System.out.println(without);
  }
}

result after startup:

{"myWorkingKey": "Hi!", "myNonWorkingKey": "}
{"myWorkingKey": "Hi!", "myNonWorkingKey": ""}


Solution

  • Why do you need to use SpEL for such a simple value? The property placeholder is enough.

    Or is this just an example based on something more complex.

    With SpEL you need triple quotes:

    myValue={"myWorkingKey": "Hi!", "myNonWorkingKey": """}