首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java 8面向jsonpath库的lambda技术问题

java 8面向jsonpath库的lambda技术问题
EN

Stack Overflow用户
提问于 2019-01-08 00:01:21
回答 1查看 155关注 0票数 4

我正在寻找关于lambda的信息,虽然我无法找到类似于下面功能的东西。它属于org.springframework.test.web.servlet.result.JsonPathResultMatchers类,ResultMatcher是@FunctionalInterface,结果是MvcResult和jsonPathHelper.doesNotExist类型的返回空

代码语言:javascript
复制
public ResultMatcher doesNotExist() {
    return result -> jsonPathHelper.doesNotExist(getContent(result));
}

我打电话到上面

代码语言:javascript
复制
jsonPath("$._embedded" ).doesNotExist()

我完全不知道:

  1. 如果jsonPathHelper.doesNotExist返回void,那么为什么doesNotExist返回ResultMatcher。
  2. 类具有类似于结果的任何内容,这个参数从何而来?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-08 00:37:32

代码中的lambda:

代码语言:javascript
复制
result -> jsonPathHelper.doesNotExist(getContent(result));

只是ResultMatcher的一个表示形式,因为它是一个FunctionalInterface。你可以把它看作是:

代码语言:javascript
复制
public ResultMatcher doesNotExist() {
    return new ResultMatcher() {
        @Override
        public void match(MvcResult result) throws Exception {
            jsonPathHelper.doesNotExist(getContent(result)); // returns void
        }
    };
}

如果jsonPathHelper.doesNotExist返回空,那么为什么doesNotExist返回ResultMatcher

您的方法doesNotExist,只返回函数接口本身,然后可以用来调用它的match函数。注意,调用也将返回void

类具有类似于结果的任何内容,这个论点从何而来?

如果您查看上面的匿名类,使用lambda表示,result将成为match方法在ResultMatcher实现中的参数。

因此,当您实际希望访问此实现(或一般情况下的ResultMatcher )时,将按以下方式调用该方法(简化初始化):

代码语言:javascript
复制
ResultMatcher resultMatcher =  doesNotExist(); // your method returns here
MvcResult result = new MvcResult(); // some MvcResult object
resultMatcher.match(result); // actual invocation
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54083561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档