微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Java中实现胡子lambda

如何解决如何在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 lambda的示例输入

[
            {
                "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-with-asciidoctor-customizing-tables-formatting-problems

关闭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());

AscidoctorTableCellContentLambda的类如下:

/*
 * 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);
        }
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。