在项目中有时候需要在yml配置文件中配置map,但是有些符号如果使用在map的key中,这些符号就会消失。下面看一下演示:
配置文件:
local:
map:
'111|222': 1
然后通过配置类读取:
配置类(需要引入spring-boot-configuration-processor依赖):
@Configuration
@ConfigurationProperties(prefix = "local")
public class LocalMapConfig {
private Map<String, Long> map;
public Map<String, Long> getMap() {
return map;
}
public void setMap(Map<String, Long> map) {
this.map = map;
}
}
读取:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BootTest {
@Autowired
private LocalMapConfig localMapConfig;
@Test
public void readMap() {
Map<String, Long> map = localMapConfig.getMap();
System.out.println(map);
}
}
看结果:

map的key中的“|”不见了,我们把“|”换成“,”试一下:

再看结果:

还是消失了!
这个跟key是否是数字没有关系,亲测把数字换成字母,符号还是会消失。
经过测试,常用的符号,会消失的和不会消失的如下:
会消失:
- '|'
- ','
- '+'
- '='
- ''
- '/'
- '~'
- '*'
- ':'
不会消失: - '.'
- '-'
就试了这些常用的,可见大部分符号都是会消失的,使用时还需注意。
网友评论