[英]Unicode escape characters are not getting parsed in Spring application.yml properties
I am trying to add a property value in application.yml
file in Spring Boot which contains a trailing space.我试图在包含尾随空格的 Spring Boot 中的application.yml
文件中添加一个属性值。 I'm retrieving the value using the @Value
annotation.我正在使用@Value
注释检索值。 Leaving the space unescaped simply trims it and adding a Unicode escape character results in the following String Example\
being parsed by the application.保留未转义的空间只是修剪它并添加 Unicode 转义字符会导致应用程序解析以下字符串Example\
。
Initialising a String variable with \
directly in the code results in an expected space character.直接在代码中使用\
初始化 String 变量会产生预期的空格字符。
Unicode escapes in strings are only accepted in double-quoted strings in YAML , so "Example\ "
should work.字符串中的 Unicode 转义仅在 YAML 中的双引号字符串中接受,因此"Example\ "
应该可以工作。
But simply quoting the string should also be sufficient: "Example "
.但简单地引用字符串也应该足够了: "Example "
。
If you want to use Unicode escaped symbols - what worked for me is that you encode the entire string.如果您想使用 Unicode 转义符号 - 对我有用的是您对整个字符串进行编码。 Also, try to use '\ ' instead of '\ ' that symbol is known as "Non-breakable white space" (in HTML - ) this definitely will not be trimmed out此外,尝试使用 '\ ' 而不是 '\ ' 该符号被称为“不可破坏的空白”(在 HTML 中 - )这绝对不会被修剪掉
Obviously, there are several sites on the internet that can do that for you, but I also wrote an Open-source library that has a util that can encode plain text strings into Unicode-escaped strings and vice-versa.显然,互联网上有几个站点可以为您执行此操作,但我还编写了一个开源库,该库具有一个 util,可以将纯文本字符串编码为 Unicode 转义字符串,反之亦然。 I found it to be very useful also when debugging various encoding issues.我发现它在调试各种编码问题时也非常有用。 In any case if you want to use it here how the code would look like:在任何情况下,如果您想在这里使用它,代码会是什么样子:
result = "Example ";
result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
System.out.println(result);
result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
System.out.println(result);
The result would be:结果将是:
\u0045\u0078\u0061\u006d\u0070\u006c\u0065\u0020
Example
The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc该库可以在Maven Central或Github 找到它作为 Maven 工件和源代码和 javadoc
Here is javadoc for the class StringUnicodeEncoderDecoder这是StringUnicodeEncoderDecoder类的javadoc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.