简体   繁体   English

Spring Boot从application.yml注入Map时不包含特殊字符

[英]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> .我有一个简单的Spring Boot项目,旨在从application.yml读取和转换数据并将其转换为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. Spring(或Spring Boot)无法按我的要求工作。 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 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 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.第一个Map( barFilters )键不包含@字符。 Another things I tried:我尝试过的另一件事:

-> to add double quotes ( " ) both sides. ->在两边加上双引号( " )。

-> to use @ with escape character ( \\@ ). ->使用带转义符( \\@ )的\\@

-> to use Unicode equivalent of @ ( U+0040 ). ->使用等效于@( U+0040 )的Unicode。

-> to use Escape sequence equivalent of @ ( \@ ). ->使用等效于@( \@ )的转义序列。

-> to use HTML code equivalent of @ ( &#64; ). ->使用等效于@( &#64; )的HTML代码。

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> .我有一个简单的Spring Boot项目,旨在从application.yml读取和转换数据并将其转换为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. Spring(或Spring Boot)无法按我的要求工作。 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 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 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.第一个Map( barFilters )键不包含@字符。 Another things I tried:我尝试过的另一件事:

-> to add double quotes ( " ) both sides. ->在两边加上双引号( " )。

-> to use @ with escape character ( \\@ ). ->使用带转义符( \\@ )的\\@

-> to use Unicode equivalent of @ ( U+0040 ). ->使用等效于@( U+0040 )的Unicode。

-> to use Escape sequence equivalent of @ ( \@ ). ->使用等效于@( \@ )的转义序列。

-> to use HTML code equivalent of @ ( &#64; ). ->使用等效于@( &#64; )的HTML代码。

None of them have worked.他们都没有工作。

Why this happens and how can I solve this?为什么会发生这种情况,我该如何解决呢? Is this a bug or a feature?这是错误还是功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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