Spring应用程序属性忽略字符串中的斜杠

时间:2018-07-11 16:00:44

标签: spring spring-mvc spring-boot

使用2.0.3.RELEASE将外部属性加载到spring boot(application.yaml)应用程序时,我遇到奇怪的行为。我将展示一个最小的例子。我定义了一个@ConfigurationProperties类,如下所示:

@Component
@ConfigurationProperties
class ConfigProperties {
    var testString: String = ""
    var testMap : MutableMap<String, String> = mutableMapOf()
}

我也有足够的application.yaml文件:

testString: "/profile/test"
testMap:
    "/profile/main" : "Welcome/to/profile"

经过评估,ConfigProperties.testString会按预期生成/profile/test,但是ConfigProperties.testMap会生成一个大小为1的映射,其中唯一条目的键设置为profilemain,值设置为{{1 }}。为什么键会忽略正斜杠(Welcome/to/profile)但值已正确解析?我试图转义正斜杠,但这没有用。我还发现其他特殊字符,例如冒号(/)和反斜杠(:)也被map键忽略。

1 个答案:

答案 0 :(得分:1)

我知道这已经很老了,但是如果其他人想知道,这就是在Spring Boot 2中的工作方式。

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#relaxed-binding

For map keys with non-alphanumeric characters (other than -) in them, surround the key name with []. For example:
spring:
  my-example:
    '[foo.baz]': bar
    '[abc xyz]': def
相关问题