简体   繁体   English

Unicode 转义字符未在 Spring application.yml 属性中解析

[英]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 CentralGithub 找到它作为 Maven 工件和源代码和 javadoc

Here is javadoc for the class StringUnicodeEncoderDecoder这是StringUnicodeEncoderDecoder类的javadoc

暂无
暂无

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

相关问题 Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties Spring Boot application.yml和application.properties - Spring Boot application.yml and application.properties 如何在springboot中从application.yml中读取带有特殊字符的属性 - How to read properties with special characters from application.yml in springboot 从 application.yml 读取属性时忽略 Spring 配置文件 - Spring profile is ignored when reading properties from application.yml "'application.yml' 中的 Spring Boot 属性未从 JUnit 测试加载" - Spring Boot properties in 'application.yml' not loading from JUnit Test Spring 引导 2 忽略在 application.yml 中设置的 HikariCP 属性 - Spring Boot 2 is ignoring HikariCP properties set in application.yml 根据application.yml的属性注入Spring Boot - Inject with Spring Boot depending on properties from application.yml Spring Boot - 如何将 application.yml 属性定义为 application.properties - Spring Boot - How to define application.yml properties as application.properties 使用 application.yml/properties 的批处理侦听器的 Spring kafka 集成属性 - Spring kafka integration properties for batch listener using application.yml/properties 如何在spring application.yml或application.properties中使用特殊字符($)作为属性键 - How to use special character ($) in spring application.yml or application.properties as a property key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM