如何在java中实现小胡子lambda



我想用java实现一个lambda,它可以从胡子模板中调用。到目前为止,我只能找到一篇关于这个主题的写得很差的博客文章:https://spring.io/blog/2016/11/21/the-joy-of-mustache-server-side-templates-for-the-jvm.本质上,我有一个模板文件

{{#getContentType}}
{{http-response.headers}}
{{/getContentType}}

方法名称为getContentType,它采用列表对象http-response.headers并返回键值对中的值

{
"name": "Content-Type",
"value": "application/json"
}

我甚至看过文件:https://github.com/spullara/mustache.java#readmehttps://github.com/spullara/mustache.java#readme,但我仍在努力弄清楚这一点。如有任何帮助,我们将不胜感激!

作为参考,getContentTypeλ的示例输入类似

[
{
"name": "Content-Type",
"value": "application/json"
},
{
"name": "Content-Length",
"value": "363"
},
{
"name": "Authorization",
"value": "Bearer bearerToken"
},
{
"name": "Host",
"value": "host"
}
]

没有任何使用TemplateFunctions的示例,但文档在这里:http://spullara.github.io/mustache/apidocs/.对于一个新人来说;只要得到它";。

我能够通过引用lambda函数上的Spring Docs来解决这个问题。问题是他们有一个错别字,上面写着";lamba";不是lambda:https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#working-具有asciidoctor自定义表格式问题。

正在关闭tabelCellContent lambda函数。我在StandardWriterReslover的测试中找到了一个例子。https://www.codota.com/code/java/classes/org.springframework.restdocs.snippet.StandardWriterResolver.

lambda函数被添加到映射templateContext:

Map<String, Object> templateContext = new HashMap<>();
templateContext.put("tableCellContent",
new AsciidoctorTableCellContentLambda());

AsciiddoctorTableCellContentLambda的类看起来像:

/*
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.templates.mustache;
import java.io.IOException;
import java.io.Writer;
import org.springframework.restdocs.mustache.Mustache.Lambda;
import org.springframework.restdocs.mustache.Template.Fragment;
/**
* A {@link Lambda} that escapes {@code |} characters so that the do not break the table's
* formatting.
*
* @author Andy Wilkinson
* @since 1.1.0
*/
public final class AsciidoctorTableCellContentLambda implements Lambda {
@Override
public void execute(Fragment fragment, Writer writer) throws IOException {
String output = fragment.execute();
for (int i = 0; i < output.length(); i++) {
char current = output.charAt(i);
if (current == '|' && (i == 0 || output.charAt(i - 1) != '\')) {
writer.append('\');
}
writer.append(current);
}
}
}

相关内容

  • 没有找到相关文章

最新更新