带有 URL 的 Spring Boot YAML 配置在版本 2 中不再正确加载 [英] Spring Boot YAML configuration with URL in key no longer loads correctly with version 2
问题描述
我正在将我的应用程序从 Spring Boot 1.5 迁移到 2.0,并且其中一个 YAML 属性不再正确加载.以下配置片段:
I'm migrating my application from Spring Boot 1.5 to 2.0 and one of the YAML properties no longer loads correctly. The following configuration snippet:
myapp
serviceUrls:
'https://example.org/test': 'https://test.example.org/Endpoint'
映射到这个配置类:
@ConfigurationProperties(prefix = "myapp", ignoreUnknownFields = false)
public final class MyAppProperties {
private Map<String, String> serviceUrls = new HashMap<>();
//[...]
}
- 使用 Spring Boot 1.5,它加载为带有
https://example.org/test
->https://test.example.org/Endpoint
的地图; - 但在 Spring Boot 2.0 中,冒号和斜线从映射键中消失
httpsexample.orgtest
->https://test.example.org/Endpoint
.立>- With Spring Boot 1.5, it loads as a map with
https://example.org/test
->https://test.example.org/Endpoint
; - but with Spring Boot 2.0, the colons and slashes disappear from map keys
httpsexample.orgtest
->https://test.example.org/Endpoint
.
我在 迁移指南.YAML 解析在 Spring Boot 2 中是否发生了变化?有没有更好的方法来编写以 URL 为键的 YAML 映射?
I couldn't find any mention of this in the migration guide. Has YAML parsing changed in Spring Boot 2? Is there a better way to write YAML maps with URLs as keys?
推荐答案
我应该检查 GitHub 问题...有人报告 类似的问题.解决方法是使用括号语法",不幸的是 几乎没有记录,将键括在括号内:
I should have checked GitHub issues... someone reported a similar problem. The solution is to use the "bracket syntax", which is unfortunately barely documented, wrapping the keys within brackets:
myapp serviceUrls: '[https://example.org/test]': 'https://test.example.org/Endpoint'
这篇关于带有 URL 的 Spring Boot YAML 配置在版本 2 中不再正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- With Spring Boot 1.5, it loads as a map with