Home > Net >  Something wrong when i used SpelExpressionParser to parse a expression with enum
Something wrong when i used SpelExpressionParser to parse a expression with enum

Time:10-14

Code show as below:

public class SpELTest {
    public static void main(String[] args) {
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("targetType", FavorTargetType.ARTICLE);
        String exp = "#{targetType.getCode()}";
        SpelExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression(exp, new TemplateParserContext());
        // SpelEvaluationException: EL1007E: Property or field 'targetType' cannot be found on null
        String value = expression.getValue(context, String.class);
        System.out.println(value);
    }
}

@Getter
@AllArgsConstructor
public enum FavorTargetType implements BaseEnum {
    ARTICLE(1, "article"),
    POSTS(2, "posts"),
    COMMENT(3, "comment")
    ;
    private final int code;
    private final String name;
}
public interface BaseEnum {
    @JsonValue
    int getCode();
    String getName();
}

When I run the code I will get a SpelEvaluationException: EL1007E: Property or field 'targetType' cannot be found on null. But as you can see, targetType is not null. So where am I going wrong? Please help me! Thanks!

CodePudding user response:

try this it is working

package com.example.test;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class SpELTest {
    public static void main(String[] args) {
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("targetType", FavorTargetType.ARTICLE);
        String exp = "#targetType.getCode()";
        SpelExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression(exp);
        // SpelEvaluationException: EL1007E: Property or field 'targetType' cannot be found on null
        String value = expression.getValue(context,String.class);
        System.out.println(value);
    }
    @Getter
    @AllArgsConstructor
    public enum FavorTargetType implements BaseEnum {
        ARTICLE(1, "article"),
        POSTS(2, "posts"),
        COMMENT(3, "comment")
        ;
        private final int code;
        private final String name;
    }
    public interface BaseEnum {
        @JsonValue
        int getCode();
        String getName();
    }

}





CodePudding user response:

String exp = "#targetType.getCode()";
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(exp);
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("targetType", FavorTargetType.ARTICLE);
String value = expression.getValue(context, String.class);
System.out.println(value);  // <--- prints out 1

Is there a specific reason you were passing new TemplateParserContext() to the parseExpression() method? Because when you do pass that argument it means you want to parse template and that is not what you are doing in this case.

Supports parsing templates as well as standard expression strings.

^ Taken from docs we can clearly see that this method can be used when parsing both templates and expression strings, you were using the right function with wrong parameters.

  • Related