Search code examples
springscalaspring-el

SpEL not able to extract attribute value from Scala object


I have a simple Scala class called Case

case class Case(
             @(Id@field) var id: String,
             var state: CaseState = new OpenCaseState,
             var notes: List[CaseNote] = new ArrayList(),
             var assignedGroups:Set[String] = new HashSet(),
             var aclTemplateIds: Set[String] = new HashSet()
           ) extends Serializable { }

I created an instance of this class called a_case, setting id as 123. I am trying to get the value of the id attribute. I tried this

var parser: ExpressionParser = new SpelExpressionParser
var context: EvaluationContext = new StandardEvaluationContext(a_case)
var extractedId = parser.parseExpression("'id'").getValue(context).asInstanceOf[String]

All I get is "id" in my extractedId variable. When I try to parse "id" without the single quotes, I get an exception saying the property id is not found in Case. Am I missing something here or is this a Scala issue?


Solution

  • SpEL can do that for you if your id has getter.

    I'm not well with Scala, but:

    BeanProperty

    You can annotate vals and vars with the @BeanProperty annotation. This generates getters/setters that look like POJO getter/setter definitions. If you want the isFoo variant, use the BooleanBeanProperty annotation. The ugly foo$_eq becomes

    setFoo("newfoo");
    getFoo();
    

    https://twitter.github.io/scala_school/java.html