【发布时间】:2020-03-10 15:20:38
【问题描述】:
是否可以将:;/,.等字符存储在Java 通过application.yml映射的键中?
我正在尝试通过 application.yml 文件中的映射来存储 URI 和与之关联的值,但是由于它是一个键,它不会保存任何特殊字符
http://localhost:8080 变为 httplocalhost8080
有什么办法可以保存那些丢失的字符吗?
~~编辑~~ 我在键周围尝试了转义字符、双引号和单引号,但一旦它们在哈希图中,它们就会被忽略。
yml文件中的属性有这样的结构:
uri-example-config:
keyToValueMap:
http://localhost:8080 : String1
http://exmaple.com: String2
http://moreURIs.com: String2
然后我的配置是:
@Configuration
@ConfigurationProperties(prefix = "uri-example-config")
@EnableConfigurationProperties
@Getter
@Setter
public class URIProperties {
private Map<String, String> keyToValueMap = new HashMap<>();
}
~~~~编辑2~~~~
是的,我可以轻松地 map.put("http://localhost:8080"...) 并且字符串按预期保留。 通过带有配置和属性的 yml 文件会导致此问题。
【问题讨论】:
-
所以您想将
http://localhost:8080作为 yaml 映射中的键?您是否尝试过使用 " 或 ` 符号转义序列? -
@michalk 是的,没有改变任何东西。上面贴了一个示例代码
标签: java spring hashmap key special-characters