简体   繁体   English

如何在springboot中从application.yml中读取带有特殊字符的属性

[英]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
  1. While reading (Samsung+Vodafone)key from application yml file , we are getting.从应用程序 yml 文件中读取 (Samsung+Vodafone) 密钥时,我们得到了。 concatenated String format as 'SamsungVodafone' .连接字符串格式为 'SamsungVodafone' 。

  2. 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 但结果是一样的,我们尝试了其他符号,例如 '-' 所以它工作正常。

  3. 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}

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.绑定到 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.

相关问题 java 8 Springboot 如何在注释中使用 application.yml 文件中的属性? - java 8 Springboot How to use properties from application.yml file in annotations? 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application 如何从 application.yml 映射时间属性? - How to map time properties from application.yml? 如何从 application.yml 文件注入属性? - How to inject properties from application.yml file? 无法访问 springboot 组件中的 application.yml 属性 - Not able to access application.yml properties in springboot component 使用application.yml配置SpringBoot - SpringBoot Configuration with application.yml 如何从 application.yml 中读取 Long 数字 - How to read a Long number from application.yml 如何在spring application.yml或application.properties中使用特殊字符($)作为属性键 - How to use special character ($) in spring application.yml or application.properties as a property key Spring Boot从application.yml注入Map时不包含特殊字符 - Spring Boot doesn't include special characters while injecting Map from application.yml Unicode 转义字符未在 Spring application.yml 属性中解析 - Unicode escape characters are not getting parsed in Spring application.yml properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM