如何在Java中使用Mustache模板Lambda解析字符串数组

huangapple go评论75阅读模式
英文:

How to parse a string array with Mustache Template Lambda in java

问题

以下是翻译好的内容:

我目前正在实现一个 Mustache Lambda 来解析这个对象:
[{name=Content-Type, value=application/hal+json;charset=UTF-8}, {name=Content-Length, value=906}]

我的模板 Lambda 如下所示:

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

public class GetContentType implements Lambda {
  @Override
  public void execute(Fragment fragment, Writer writer) throws IOException {
    fragment.execute(writer);
  }
}

Mustache 模板如下所示:

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

生成模板的输出产物如下所示:

[{name=Content-Type, value=application/hal+json;charset=UTF-8}, {name=Content-Length, value=906}]

我正在尝试找出如何从 Java 函数中的键 Content-Type 返回值 application/hal+json。关于这方面的文档不多,因此非常感谢任何帮助。

英文:

I am currently working on implementing a Mustache Lambda to parse this object:
[{name=Content-Type, value=application/hal+json;charset=UTF-8}, {name=Content-Length, value=906}]

My template lambda looks like this:

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


public class GetContentType implements Lambda {
  @Override
  public void execute(Fragment fragment, Writer writer) throws IOException {
    fragment.execute(writer);
  }
}

The mustache template looks like this

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

The output from generating the template produces the following

[{name=Content-Type, value=application/hal+json;charset=UTF-8}, {name=Content-Length, value=906}]

I am trying to figure out how to return the value application/hal+json from the key Content-Type in the java function. There isn't much documentation on this so any help would be greatly appreciated.

答案1

得分: 1

被传递到你的 Lambda 中的 Fragment 通过 context() 方法可以获取上下文。上下文的确切类型和内容取决于你的 Lambda 的使用方式,因此它被标记为 Object

看起来你正在处理由 Spring REST Docs 添加到上下文中的 headers。在这种情况下,头部是一个 List,其中每个 Map 包含 namevalue 条目。

希望这足够让你从 Fragment 中检索 context(),并开始对其内容进行转换并深入查找带有 name=Content-Type 条目的映射,从而可以检索以 value 为键的条目。

英文:

The Fragment that is passed into your Lambda has a context that's available via the context() method. The exact type and content of the context depends on how your lambda's being used so it's typed as an Object.

It looks like you're working with the headers that are added to the context by Spring REST Docs. In this case the headers are a List of Map, where each map contains name and value entries.

Hopefully this gives you enough to retrieve the context() from the Fragment and start casting it and drilling down into its content to find the map with a name=Content-Type entry from where you can retrieve the entry that's keyed with value.

huangapple
  • 本文由 发表于 2020年10月22日 01:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64468678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定