Java SpEL EL1043E:意外的令牌。期待' rparen())'但是是逗号(,)'

时间:2018-02-17 11:49:34

标签: java spring java-8 spring-el

我尝试学习SpEL,但我的一个脚本不起作用。 我的剧本是:

changePages(#{T(StoryContent).pages}, (x, y) -> x.replaceAll(y, x.substring(0, 1) + '...' + x.substring(x.length() - 1, x.length())))

Java代码:

     public void validate(Story story, List<StoryRule> rules) throws NoSuchMethodException, SecurityException {
            StoryContent storyContent = storyContentRepository.findOne(story.getContentId());
            story.setStatus(SchedulerStatus.VALIDATION);
            storyRepository.save(story);
            ExpressionParser parser = new SpelExpressionParser();
            StandardEvaluationContext inventorContext = new StandardEvaluationContext(storyContent);
            inventorContext.registerFunction("changePages", ValidationByRuleServiceImpl.class
                    .getDeclaredMethod("changePages", new Class[] { List.class, BiFunction.class }));
            try {
                rules.stream().map(StoryRule::getScript)
                        .forEach(script -> parser.parseExpression(script).getValue(inventorContext));
                if (!storyContent.getTitle().equals(story.getName()))
                    story.setName(storyContent.getTitle());
                story.setStatus(SchedulerStatus.PUBLISHED);
                storyContentRepository.save(storyContent);
            } catch (Exception e) {
                LOG.error(e.getMessage());
                story.setStatus(SchedulerStatus.ERROR);
            } finally {
                storyRepository.save(story);
            }
    }
    private void changePages(List<String> pages, BiFunction<String, String, String> function) {
        pages.forEach(System.out::println);
    }

错误是:

Expression [changePages(T(StoryContent).pages, (x, y) -> x.replaceAll(y, x.substring(0, 1) + '...' + x.substring(x.length() - 1, x.length())))] @37: EL1043E: Unexpected token. Expected 'rparen())' but was 'comma(,)'

我从DB获得SpEL脚本列表。

我不知道什么是错的,我的脚本或我的java代码?

1 个答案:

答案 0 :(得分:5)

SpEL不是Java,它是一种不同的语言;首字母缩略词代表Spring Expression 语言

它不理解Java8 lambda,因此无法解析(x, y) -> ...

但是,您可以将lambdas注册为SpEL函数,请参阅my answer here