Serving Alternative Formats

环境端点的默认 JSON 格式非常适合 Spring 应用程序使用,因为它可以直接映射到 Environment 抽象。如果您愿意,可以通过在资源路径中添加后缀(“.yml”、“.yaml”或 “.properties”),将相同的数据当做 YAML 或 Java 属性来使用。对于不关心 JSON 端点结构或它们提供的额外元数据(例如,不使用 Spring 的应用程序可能会受益于这种方法的简单性)的应用程序,这可能很有用。

The default JSON format from the environment endpoints is perfect for consumption by Spring applications, because it maps directly onto the Environment abstraction. If you prefer, you can consume the same data as YAML or Java properties by adding a suffix (".yml", ".yaml" or ".properties") to the resource path. This can be useful for consumption by applications that do not care about the structure of the JSON endpoints or the extra metadata they provide (for example, an application that is not using Spring might benefit from the simplicity of this approach).

YAML 和属性表示形式有一个附加标志(作为名为 resolvePlaceholders 的布尔查询参数提供),当 possible 时,表示应在源文档(以 Spring 的标准 ${…​} 形式)中解决占位符,然后进行渲染。对于不了解 Spring 占位符约定的使用者而言,这是个有用的功能。

The YAML and properties representations have an additional flag (provided as a boolean query parameter called resolvePlaceholders) to signal that placeholders in the source documents (in the standard Spring ${…​} form) should be resolved in the output before rendering, where possible. This is a useful feature for consumers that do not know about the Spring placeholder conventions.

使用 YAML 或属性格式存在一些限制,主要与元数据的丢失有关。例如,JSON 被构建为一个有序的属性源列表,其中名称与源相关。即使值的来源有多个,YAML 和属性形式也会合并到一个单一映射中,并且原始源文件的文件名也会丢失。同样,YAML 表示也不一定是支持存储库中 YAML 源的真实表示。它是基于一个扁平属性源列表构造的,并且必须对键的形式做出假设。

There are limitations in using the YAML or properties formats, mainly in relation to the loss of metadata. For example, the JSON is structured as an ordered list of property sources, with names that correlate with the source. The YAML and properties forms are coalesced into a single map, even if the origin of the values has multiple sources, and the names of the original source files are lost. Also, the YAML representation is not necessarily a faithful representation of the YAML source in a backing repository either. It is constructed from a list of flat property sources, and assumptions have to be made about the form of the keys.