简体   繁体   中英

Spring Boot doesn't include special characters while injecting Map from application.yml

I have a simple Spring Boot project that aims to read and convert data from application.yml and convert it to a Java Map<String,String> . In my project's business layer, I need to fill that map with email addresses as keys. The problem starts here. Spring (or Spring Boot) does not work as I wanted. It totally ignores @ characters in key side but works properly in values side.

Here are some codes:

Application.java

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

Bootstrap.java

@Slf4j
@Component
@ConfigurationProperties(prefix = "foo-filter")
public class Bootstrap implements CommandLineRunner {

    @Setter
    private Map<String, String> barFilters;

    @Setter
    private Map<String, String> bazFilters;

    @Override
    public void run(String... args) {
        barFilters.forEach((key, value) -> log.info(key + " -> " + value));

        bazFilters.forEach((key, value) -> log.info(key + " -> " + value));
    }
}

application.yml

foo-filter:
  bar-filters:
    filtered1@mail.com: all
    filtered2@mail.com: all
    filtered3@mail.com: some
    filtered4@mail.com: none

  baz-filters:
    filtered1: all@mail.com
    filtered2: none@mail.com

And finally, this is the console output:

filtered1mail.com -> all
filtered2mail.com -> all
filtered3mail.com -> some
filtered4mail.com -> none
filtered1 -> all@mail.com
filtered2 -> none@mail.com

The first Map ( barFilters ) keys doesn't include @ characters. Another things I tried:

-> to add double quotes ( " ) both sides.

-> to use @ with escape character ( \\@ ).

-> to use Unicode equivalent of @ ( U+0040 ).

-> to use Escape sequence equivalent of @ ( \@ ).

-> to use HTML code equivalent of @ ( &#64; ).

None of them have worked.

Why this happens and how can I solve this? Is this a bug or a feature?

I have a simple Spring Boot project that aims to read and convert data from application.yml and convert it to a Java Map<String,String> . In my project's business layer, I need to fill that map with email addresses as keys. The problem starts here. Spring (or Spring Boot) does not work as I wanted. It totally ignores @ characters in key side but works properly in values side.

Here are some codes:

Application.java

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

Bootstrap.java

@Slf4j
@Component
@ConfigurationProperties(prefix = "foo-filter")
public class Bootstrap implements CommandLineRunner {

    @Setter
    private Map<String, String> barFilters;

    @Setter
    private Map<String, String> bazFilters;

    @Override
    public void run(String... args) {
        barFilters.forEach((key, value) -> log.info(key + " -> " + value));

        bazFilters.forEach((key, value) -> log.info(key + " -> " + value));
    }
}

application.yml

foo-filter:
  bar-filters:
    filtered1@mail.com: all
    filtered2@mail.com: all
    filtered3@mail.com: some
    filtered4@mail.com: none

  baz-filters:
    filtered1: all@mail.com
    filtered2: none@mail.com

And finally, this is the console output:

filtered1mail.com -> all
filtered2mail.com -> all
filtered3mail.com -> some
filtered4mail.com -> none
filtered1 -> all@mail.com
filtered2 -> none@mail.com

The first Map ( barFilters ) keys doesn't include @ characters. Another things I tried:

-> to add double quotes ( " ) both sides.

-> to use @ with escape character ( \\@ ).

-> to use Unicode equivalent of @ ( U+0040 ).

-> to use Escape sequence equivalent of @ ( \@ ).

-> to use HTML code equivalent of @ ( &#64; ).

None of them have worked.

Why this happens and how can I solve this? Is this a bug or a feature?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM