gpt4 book ai didi

java - 如何在 spring boot 中从 application.yml 中读取带有特殊字符的属性

转载 作者:搜寻专家 更新时间:2023-10-31 20:16:37 24 4
gpt4 key购买 nike

应用程序.yml

mobile-type:
mobile-codes:
BlackBerry: BBSS
Samsung: SAMS
Samsung+Vodafone: SAMSVV
  1. 从应用程序 yml 文件中读取 (Samsung+Vodafone)key 时,我们得到了。连接的字符串格式为“SamsungVodafone”。

  2. 此外,我们尝试过“三星”/+“沃达丰”:SAMSVV,但结果是一样的,我们还尝试了其他符号,例如“-”,所以它工作正常。

  3. 用于从应用程序 yml 文件中读取键和值。我们写了下面的代码。

import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "mobile-type")
@Component
public class mobileTypeConfig {


Map<String, String> mobileCodes;

public Map<String, String> getMobileCodes() {
return mobileCodes;
}

public void setMobileCodes(Map<String, String> mobileCodes) {
this.mobileCodes= mobileCodes;
}
}

注:Spring Boot版本2.0.6.发布

最佳答案

使用方括号不转义任何字符并用双引号将其编码

mobile-type:
mobile-codes:
BlackBerry: BBSS
Samsung: SAMS
"[Samsung+Vodafone]": SAMSVV

输出

{BlackBerry=BBSS, Samsung=SAMS, Samsung+Vodafone=SAMSVV}

Binding

When binding to Map properties, if the key contains anything other than lowercase alpha-numeric characters or -, you need to use the bracket notation so that the original value is preserved. If the key is not surrounded by [], any characters that are not alpha-numeric or - are removed. For example, consider binding the following properties to a Map:

acme:
map:
"[/key1]": value1
"[/key2]": value2

关于java - 如何在 spring boot 中从 application.yml 中读取带有特殊字符的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57147000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com