【问题标题】:How to implement mustache lambda in java如何在 java 中实现 mustache lambda
【发布时间】:2020-10-16 15:27:48
【问题描述】:

我想在 java 中实现一个可以从 mustache 模板调用的 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/。一个新人很难“得到它”。

【问题讨论】:

    标签: java mustache


    【解决方案1】:

    我可以通过参考 Spring Docs on lambda 函数来解决这个问题。问题是他们有一个错字,上面写着“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());
    

    AsciidoctorTableCellContentLambda 的类如下所示:

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

    【讨论】:

      猜你喜欢
      • 2012-02-11
      • 2023-03-30
      • 2016-11-15
      • 1970-01-01
      • 2021-09-08
      • 2011-05-13
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多