Home > Back-end >  The $ when used in a key name in yml disappears
The $ when used in a key name in yml disappears

Time:03-24

I am using application.yml within a springboot application and loading a map. Unfortunately, whenever I have a $ character in the key, it simply disappears. The key/value pair is there, just without any $ signs. I've tried various ways of attempting to escape the character but nothing seems to work. I can't see any documentation that states that $ is a special character - except perhaps within classpath notation? Is this what is happening here? And if it is, how do I escape it?

first:
  second:
    third:
      'aaa': "aaa"
      '$bbb': "bbb"
      '$ccc': "ccc"
      $$yy$y: "yyyy"

In this example, the map is defined as

private Map<String, String> third;

CodePudding user response:

This works:

first:
  second:
    third:
      "[aaa]": "aaa"
      "[$bbb]": "bbb"
      "[$ccc]": "ccc"
      "[$$yy$y]": "yyyy"
{aaa=aaa, $bbb=bbb, $ccc=ccc, $$yy$y=yyyy}
@SpringBootApplication
@EnableConfigurationProperties
public class So71584675Application {

    public static void main(String[] args) {
        SpringApplication.run(So71584675Application.class, args);
    }

    @Bean
    ApplicationRunner runner(Props props) {
        return args -> {
            System.out.println(props.third);
        };
    }

}

@Component
@ConfigurationProperties(prefix = "first.second")
class Props {

    Map<String, String> third;

    Props(Map<String, String> third) {
        this.third = third;
    }

    Map<String, String> getThird() {
        return this.third;
    }

    void setThird(Map<String, String> third) {
        this.third = third;
    }

}
  • Related