如何在java中实现mustachelambda
我想用java实现一个lambda,它可以从一个mustache模板调用。到目前为止,我只能找到一篇写得很差的关于这个主题的博客文章:。基本上,我有一个模板文件如何在java中实现mustachelambda,java,mustache,Java,Mustache,我想用java实现一个lambda,它可以从一个mustache模板调用。到目前为止,我只能找到一篇写得很差的关于这个主题的博客文章:。基本上,我有一个模板文件 {{#getContentType}} {{http-response.headers}} {{/getContentType}} 方法名为getContentType它接受一个列表对象http响应.headers,并从键值对返回值 { "name": "Content-Type&quo
{{#getContentType}}
{{http-response.headers}}
{{/getContentType}}
方法名为getContentType
它接受一个列表对象http响应.headers
,并从键值对返回值
{
"name": "Content-Type",
"value": "application/json"
}
我甚至看过文档:,但我仍在努力弄清楚这一点。任何帮助都将不胜感激
如需参考,getContentType
lambda的输入示例
[
{
"name": "Content-Type",
"value": "application/json"
},
{
"name": "Content-Length",
"value": "363"
},
{
"name": "Authorization",
"value": "Bearer bearerToken"
},
{
"name": "Host",
"value": "host"
}
]
没有任何使用TemplateFunction的示例,但文档如下:。对于一个新手来说,很难“直接得到它”。我通过参考lambda函数上的Spring文档就能够理解这一点。问题是他们有一个打字错误,上面写着“lamba”而不是lambda: 退出tabelCellContent lambda函数。我在
StandardWriterReslover
的测试中找到了一个例子
lambda函数被添加到映射templateContext
:
Map<String, Object> templateContext = new HashMap<>();
templateContext.put("tableCellContent",
new AsciidoctorTableCellContentLambda());
Map templateContext=newhashmap();
templateContext.put(“tableCellContent”,
新的AscidActorTableCellContentLambda());
AscidActorTableCellContentLambda的类如下所示:
/*
* 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);
}
}
}
/*
*版权所有2014-2016原作者。
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
包org.springframework.restdocs.templates.mustache;
导入java.io.IOException;
导入java.io.Writer;
导入org.springframework.restdocs.mustache.mustache.Lambda;
导入org.springframework.restdocs.mustache.Template.Fragment;
/**
*一种{@link Lambda},它转义{@code |}个字符,以便不破坏表的
*格式化。
*
*@作者安迪·威尔金森
*@自1.1.0以来
*/
公共最终类AscidActorTableCellContentLambda实现Lambda{
@凌驾
public void execute(片段片段、编写器)引发IOException{
字符串输出=fragment.execute();
for(int i=0;i