How to read properties with special characters from application.yml in springboot

Rohan Sutar :

application.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. concatenated String format as '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 .

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

Note :Spring Boot Version 2.0.6.RELEASE

Deadpool :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=124589&siteId=1