首页 > 解决方案 > 带有密钥 URL 的 Spring Boot YAML 配置不再使用版本 2 正确加载

问题描述

我正在将我的应用程序从 Spring Boot 1.5 迁移到 2.0,并且 YAML 属性之一不再正确加载。以下配置片段:

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 2 中的 YAML 解析是否发生了变化?有没有更好的方法来编写以 URL 作为键的 YAML 映射?

标签: spring-bootyamlsnakeyaml

解决方案


我应该检查 GitHub 问题...有人报告了类似的问题。解决方案是使用“括号语法”,不幸的是几乎没有记录,将键包裹在括号内:

myapp
  serviceUrls:
    '[https://example.org/test]': 'https://test.example.org/Endpoint'

推荐阅读