I'm quite new to spring expression language. I have a map which contains values as string.
{
"id": "1",
"object": {
"object1": {
"fields": {
"value1": {
"value":"3"
},
"value2": {
"value":"2"
}
}
}
}
}
The spring expression that i have looks something like this,
((object['object1'].fields['value1'].value * object['object1'].fields['value2'].value) * 0.5)
My calculation method looks like this where SampleObject class has the map object
public Double calculation(String expression, SampleObject sampleObject){
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(expression);
Double evaluatedValue = (Double)exp.getValue(sampleObject);
return evaluatedValue;
}
Since the value is string, I get an error saying cannot convert String to Double. This can be eliminated if i change the expression like this
((new Double(object['object1'].fields['value1'].value) * new Double(object['object1'].fields['value2'].value)) * 0.5)
But I'm looking for a solution where I don't have to change the expression or the object. Is there a solution.
Thanks in advance
Since your JSON has
value1.valueandvalue2.valueas String the Expression throws the Exception.Either change your JSON to have Decimal values
Or in the Expression method instead of casting do parsing.