Spring Boot application.yml 双反斜杠按字面解释

Ada*_*lik 3 java spring yaml spring-boot

我使用的是 Spring Boot 1.5.8

我的application.yml有以下属性:

my:
  path: \\\\hostname\\dir
Run Code Online (Sandbox Code Playgroud)

根据我对YAML 规范5.7 的理解。转义字符双反斜杠\\是单个反斜杠的有效转义序列,因此上述配置应产生以下值my.path: \\hostname\dir

但是,在我的应用程序中,我可以看到该属性的值包含双反斜杠:

@RunWith(SpringRunner.class)
@SpringBootTest
public class PropTest {

    @Getter
    @Setter
    public static class MyProp {
        private String path;
    }

    @Autowired
    private MyProp my;

    @Test
    public void testProp() {
        System.out.println(my.path);
    }

    @Configuration
    @EnableConfigurationProperties
    public static class Config {
        @Bean
        @ConfigurationProperties("my")
        public MyProp my() {
            return new MyProp();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

印刷:

\\\\host\\dir
Run Code Online (Sandbox Code Playgroud)

我无法将值放入application.yml引号中,因为该文件是由配置系统 (SaltStack) 生成的,并且它有自己的 Yaml 渲染库。

这是 Spring Boot / Spring Boot 使用的 Yaml 库中的错误吗?有没有办法强制 Spring Boot 将 Yaml 中的双反斜杠视为单个反斜杠的转义?

fly*_*lyx 5

引用您链接的规格部分:

\n\n
\n

请注意,转义序列仅在双引号标量中解释。在所有其他标量样式中,\xe2\x80\x9c\\\xe2\x80\x9d 字符没有特殊含义,并且不可打印字符不可用。

\n
\n\n

因此,如果您不对标量加双引号,则\\它是一个像其他字符一样的普通字符,您可以写

\n\n
my:\n  path: \\\\hostname\\dir\n
Run Code Online (Sandbox Code Playgroud)\n