springspring-elspelevaluationexception

After upgrading spring framework 6.0.14 to 6.1.6 getting error in this spell expression


This is the spell expression:

@Cacheable(value = "Cache", 
    key = "(#request).getToken().hashCode()", 
    unless = "#result == null")

ERROR:org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getAuthenticationRequest() on null context object

How do I resolve this issue?


Solution

  • I think the answer is in the Javadocs:

    /**
     * Spring Expression Language (SpEL) expression for computing the key dynamically.
     * <p>Default is {@code ""}, meaning all method parameters are considered as a key,
     * unless a custom {@link #keyGenerator} has been configured.
     * <p>The SpEL expression evaluates against a dedicated context that provides the
     * following meta-data:
     * <ul>
     * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
     * references to the {@link java.lang.reflect.Method method}, target object, and
     * affected cache(s) respectively.</li>
     * <li>Shortcuts for the method name ({@code #root.methodName}) and target class
     * ({@code #root.targetClass}) are also available.
     * <li>Method arguments can be accessed by index. For instance the second argument
     * can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
     * can also be accessed by name if that information is available.</li>
     * </ul>
     */
    String key() default "";
    

    I assume your request is a parameter name. Apparently such an information is not available in your case. Try to compile your project with -parameters option.