在Java中使用JMustache实现lambda函数 [英] Implementing a lambda function with JMustache in java

查看:282
本文介绍了在Java中使用JMustache实现lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注JMustache的文档: https://github.com/samskivert/jmustache .它说我们可以调用Java函数并在Mustache模板中使用它们.我有一个lambda函数,像这样

I am following the documentation for JMustache: https://github.com/samskivert/jmustache. It says that we can call java functions and use them in Mustache templates. I have a lambda function like so

 Mustache.Lambda lookupInstance = new Mustache.Lambda() {
      public void execute (Template.Fragment frag, Writer out) throws IOException {
          out.write("<b>");
          frag.execute(out);
          out.write("</b>");
      }
    };

然后我有一个引用lambda的模板文件,

Then I have a template file that references the lambda like so

{{#myMethod}} is awesome.{{/myMethod}}

模板的输出如下:

is awesome.

我期待中

<b> is awesome.</b>

有人可以帮我弄清楚为什么该方法执行不正确吗?我已经尝试调试了很长时间,奇怪的是,写入Writer的所有内容都会被忽略,而frag.execute是唯一起作用的东西.该方法对Writer有什么作用?被忽略了吗?可以在碎片内部写入其他引用吗?

Can someone please help me figure out why the method isn't executing properly? I've been trying to debug this for quite some time now.It's odd that anything written to the Writer is ignored and that the frag.execute is the only thing working. What does that method do with the Writer? Is it ignored? Is there a different reference to write to inside of the frag?

推荐答案

我遇到的问题是Spring RestDocs使用Mustache作为传递依赖项.我已将JMustache添加到pom.xml中,这是多余的.我们可以通过以下代码使用lambda函数

The issue that I was running into was that Spring RestDocs uses Mustache as a transitive dependency. I had added JMustache to the pom.xml which was redundant. We can use a lambda function via the following

@Override
  public void document(Operation operation) throws IOException {
    try {
    RestDocumentationContext context = (RestDocumentationContext) operation
        .getAttributes().get(RestDocumentationContext.class.getName());

    StandardWriterResolver writerResolver = new StandardWriterResolver(
        new RestDocumentationContextPlaceholderResolverFactory(),
        "UTF-8",
        new TemplateFormat() {
          @Override public String getId() { return outFileExt; }
          @Override public String getFileExtension() { return outFileExt; }
        });
   
    Map<String,Object> data = new HashMap<>(operation.getAttributes());
    data.put("myMethod", new MyMustacheLambda());
    
    
    TemplateEngine templateEngine = 
        (TemplateEngine) data
        .get(TemplateEngine.class.getName());


    try (Writer writer = writerResolver.resolve(
        operation.getName(), 
        outFileName,
        context)) {
      writer.append(templateEngine
              .compileTemplate(this.templateName)
              .render(data));
    }
    
    }
    catch (Throwable t) {
      t.printStackTrace();
    }
  }

并通过

import java.io.IOException;
import java.io.Writer;
import org.springframework.restdocs.mustache.Mustache.Lambda;
import org.springframework.restdocs.mustache.Template.Fragment;

public class MyMustacheLambda implements Lambda {
  
  @Override
  public void execute(Fragment fragment, Writer writer) throws IOException {
      String output = fragment.execute();
      writer.write("test");
  }

}

因此,我们必须创建一个实现Lambda并覆盖execute方法的类.

So we have to create a class that implements Lambda and overrides the execute method.

这篇关于在Java中使用JMustache实现lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆