Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 356
Rohan Sutar Avatar asked Jul 22 '19 13:07

Rohan Sutar


People also ask

How do you read custom properties from application properties in Spring boot?

Another very simple way to read application properties is to use @Value annotation. Simply annotation the class field with @Value annotation providing the name of the property you want to read from application. properties file and class field variable will be assigned that value.

How do you read property on Yml application?

To read from the application. yml or property file : The easiest way to read a value from the property file or yml is to use the spring @value annotation.

How do you add special characters in Yml?

YAML doesn't require quoting most strings but you'll want to quote special characters if they fall within a certain list of characters. Use quotes if your value includes any of the following special characters: { , } , [ , ] , & , * , # , ? , | , - , < , > , = , ! , % , @ , : also ` and , YAML Spec.

How can we access the YAML properties in Spring boot?

To access the YAML properties, we'll create an object of the YAMLConfig class, and access the properties using that object. In the properties file, we'll set the spring. profiles. active environment variable to prod.


1 Answers

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
like image 85
Deadpool Avatar answered Oct 27 '22 00:10

Deadpool