[英]How to read properties with special characters from application.yml in springboot
application.yml应用程序.yml
mobile-type:
mobile-codes:
BlackBerry: BBSS
Samsung: SAMS
Samsung+Vodafone: SAMSVV
While reading (Samsung+Vodafone)key from application yml file , we are getting.从应用程序 yml 文件中读取 (Samsung+Vodafone) 密钥时,我们得到了。 concatenated String format as 'SamsungVodafone' .连接字符串格式为 'SamsungVodafone' 。
Morever we heve tried "Samsung'/+'Vodafone": SAMSVV but the result was same and we have tried other symbol such as '-' so its working fine .此外,我们尝试过 "Samsung'/+'Vodafone": SAMSVV 但结果是一样的,我们尝试了其他符号,例如 '-' 所以它工作正常。
For reading key and value from application yml file .用于从应用程序 yml 文件中读取键和值。 we have written below code.我们写了下面的代码。
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;
}
}
Note :Spring Boot Version 2.0.6.RELEASE注意:Spring Boot 版本 2.0.6.RELEASE
Use square brackets not to escape any character and encode that in double quotes使用方括号不转义任何字符并将其编码为双引号
mobile-type:
mobile-codes:
BlackBerry: BBSS
Samsung: SAMS
"[Samsung+Vodafone]": SAMSVV
Output输出
{BlackBerry=BBSS, Samsung=SAMS, Samsung+Vodafone=SAMSVV}
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.绑定到 Map 属性时,如果键包含小写字母数字字符或 - 以外的任何内容,则需要使用括号表示法,以便保留原始值。 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:例如,考虑将以下属性绑定到 Map:
acme:
map:
"[/key1]": value1
"[/key2]": value2
please keep in mind that the left side is a yml key, not an arbitrary string.请记住,左侧是一个 yml 键,而不是任意字符串。 my suggestion or your usecase would be to have a map with both on the right side such as:我的建议或您的用例是在右侧都有一张地图,例如:
foo:
- name: "Samsung+Vodafone"
code: "SAMSVV"
- name: "BlackBerry"
code: "BBMS"
- name: "Samsung"
codes:
- "SAMS"
- "SMG"
you will have to change your class structure slightly, but you could actually reconstruct your initial approach from that.你将不得不稍微改变你的类结构,但你实际上可以从中重建你的初始方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.