如何在springboot中从application.yml中读取带有特殊字符的属性 [英] How to read properties with special characters from application.yml in springboot

查看:106
本文介绍了如何在springboot中从application.yml中读取带有特殊字符的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序.yml

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

  1. 从应用程序 yml 文件中读取 (Samsung+Vodafone) 密钥时,我们得到了.连接字符串格式为 'SamsungVodafone' .

  1. While reading (Samsung+Vodafone)key from application yml file , we are getting. concatenated String format as 'SamsungVodafone' .

此外,我们还尝试过Samsung'/+'Vodafone":SAMSVV,但结果是一样的,我们也尝试过其他符号,例如-",所以它工作正常.

Morever we heve tried "Samsung'/+'Vodafone": SAMSVV but the result was same and we have tried other symbol such as '-' so its working fine .

用于从应用程序 yml 文件中读取键和值.我们已经编写了以下代码.

For reading key and value from application yml file . 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;
        }
}

注意:Spring Boot 版本2.0.6.发布

Note :Spring Boot Version 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

输出

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

绑定

绑定到 Map 属性时,如果键包含小写字母数字字符或 - 以外的任何内容,则需要使用括号表示法,以便保留原始值.如果键没有被 [] 包围,则任何非字母数字或 - 的字符都将被删除.例如,考虑将以下属性绑定到 Map:

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

这篇关于如何在springboot中从application.yml中读取带有特殊字符的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆