假设我们有一个配置属性类:
@ConfigurationProperties(prefix = "whitespace.test")
public class WhitespaceTestConfig {
private Map<String, String> configs;
public Map<String, String> getConfigs() {
return configs;
}
public void setConfigs(Map<String, String> configs) {
this.configs = configs;
}
}
Run Code Online (Sandbox Code Playgroud)
我们尝试使用包含空格的密钥对其进行配置:
whitespace.test.configs:
Key With Whitespace: "I am a value with whitespace in it"
Run Code Online (Sandbox Code Playgroud)
似乎通过 spring 可以很好地解析这个 yaml,而且它显然是有效的 yaml。但是,spring (SnakeYaml?) 删除了 Key 字符串中的空格:
KeyWithWhitespace -> I am a value with whitespace in it
Run Code Online (Sandbox Code Playgroud)
一个简单的解决方案是为空间指定一个特殊字符并在应用程序中替换它,但我想知道 spring 是否已经以某种方式处理过这个问题?也许有一种方法可以逃避配置中的空间,让 spring(SnakeYaml?)知道我们想要保留它,或者有一种方法可以配置它?
为了完整起见,我尝试使用单引号和双引号以及\s \b.
更新:
经过一些额外的研究,我发现了一个来自 SnakeYaml 存储库的例子,它似乎表明我正在寻找的东西应该是可能的:https ://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-block-mappings
具体这个例子:
# YAML …Run Code Online (Sandbox Code Playgroud)