Moc*_*ump 6 yaml snakeyaml spring-boot
假设我们有一个配置属性类:
@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
base armor class: 0
base damage: [4,4]
plus to-hit: 12
plus to-dam: 16
plus to-ac: 0
Run Code Online (Sandbox Code Playgroud)
# Java
{'plus to-hit': 12, 'base damage': [4, 4], 'base armor class': 0, 'plus to-ac': 0, 'plus to-dam': 16}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,空格仍然存在于键中。不幸的是,我对弄清楚空格实际上被删除的位置一无所知。
小智 17
对于带有特殊字符的映射键,您需要将键用'[]'括起来,以便按指定使用键。
所以,在你的情况下,它将是
whitespace.test.configs:
'[Key With Whitespace]': "I am a value with whitespace in it"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3753 次 |
| 最近记录: |