【问题标题】:How to convert from application.properties to application.yml in Spring Boot?如何在 Spring Boot 中从 application.properties 转换为 application.yml?
【发布时间】:2019-12-19 07:25:19
【问题描述】:

我有一个非常简单的 Spring Boot 应用程序和 application.properties,现在我想移动到 application.yml

这是 application.yml 文件:

spring:
    datasource:
        url: jdbc:mysql://localhost:3306/employee_directory
        username: student
        password: Password123

所以我删除了 application.properties 文件并编写了 application.yml 文件。如果我运行该应用程序,我会收到此异常:

   20:31:22.446 [restartedMain] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:524)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:473)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:443)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$null$6(ConfigFileApplicationListener.java:425)
    at java.lang.Iterable.forEach(Iterable.java:75)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$load$7(ConfigFileApplicationListener.java:425)
    at java.lang.Iterable.forEach(Iterable.java:75)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:422)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:321)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:202)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:186)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:176)
    at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:164)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:340)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
    at com.dgs.springboot.SpringBootRESTJPA.SpringBootRestJPAApplication.main(SpringBootRestJPAApplication.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
 in 'reader', line 2, column 1:
        datasource:
    ^

    at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:419)
    at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:227)
    at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingValue.produce(ParserImpl.java:586)
    at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)
    at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:148)
    at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:124)
    at org.yaml.snakeyaml.composer.Composer.composeValueNode(Composer.java:236)
    at org.yaml.snakeyaml.composer.Composer.composeMappingChildren(Composer.java:227)
    at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:215)
    at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:144)
    at org.yaml.snakeyaml.composer.Composer.getNode(Composer.java:85)
    at org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructor.java:123)
    at org.yaml.snakeyaml.Yaml$1.next(Yaml.java:547)
    at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:160)
    at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:134)
    at org.springframework.boot.env.OriginTrackedYamlLoader.load(OriginTrackedYamlLoader.java:75)
    at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:49)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadDocuments(ConfigFileApplicationListener.java:542)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:497)
    ... 28 common frames omitted

我想我需要对我的代码进行其他修改才能使用这个 application.yml 文件,但我不知道该怎么做?

这是 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dgs.springboot</groupId>
    <artifactId>SpringBootRESTHibernate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootRESTHibernate</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <!-- Add work around for Eclipse bug -->
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- This is used for packaging and running our app -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我们将不胜感激任何反馈。谢谢!

【问题讨论】:

  • 什么是全栈
  • @Darren 我刚刚更新了它
  • 消息很清楚:found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation).
  • 谢谢JB Nizet!

标签: java spring-boot yaml


【解决方案1】:

我构建了一个在线转换工具(http://env.simplestep.ca/),可以在spring boot yaml、属性和环境变量之间进行转换——使用spring boot's relaxed binding rules

【讨论】:

【解决方案2】:

检查您是否使用制表符进行缩进。 YAML spec 不允许这样做:

为了保持可移植性,缩进中不能使用制表符,因为不同的系统对制表符的处理方式不同。请注意,大多数现代编辑器都可以配置为按下 Tab 键会插入适当数量的空格。

【讨论】:

    【解决方案3】:

    您可以使用命令行工具 props2yaml 进行从属性到 yaml 的通用自动(和正确)转换。

    【讨论】:

      【解决方案4】:

      如果您使用 EclipseSTS,则非常简单。

      只需打开 .properties 文件的上下文菜单并选择“转换为 Yaml 文件”。

      文件将自动转换。如果您的上下文菜单上没有此功能,您需要从

      安装 Yaml 插件
      Help menu-> Eclipse market place.
      

      【讨论】:

      • 市场上有哪个 yaml 插件?有一堆。
      【解决方案5】:

      我认为您的 datasource.url 值需要用引号引起来,因为 ':'。试试这个:

        ...
        url: 'jdbc:mysql://localhost:3306/employee_directory'
        ...
      

      此外,正如上面所指出的,您应该使用空格进行缩进;通常,每层有两个空格。

      祝你好运!

      【讨论】:

        【解决方案6】:

        对于其他寻找类似答案的人来说,在 IntelliJ 中运行的 Spring 项目中,它只需将 application.properties 的扩展名更改为 application.yml。在我的情况下,文件是空的,所以我不需要更改任何内容,我可以正确使用 application.yml。

        【讨论】:

          【解决方案7】:

          这只是一个额外的答案,可能会对其他开发人员有所帮助。

          我找到了一个更好更简单的工具来将属性文件转换为 YML 文件。 convert from properties to yml

          你必须上传你的 .properties 文件,然后选择 To YML 文件选项,它会生成一个非常好的 yml 文件。 该工具还支持许多其他格式,例如 Yml 转 XML、Yml 转属性、CSV、JSON 等。

          【讨论】:

            【解决方案8】:

            文件application.yml必须在根文件夹resources中。

            【讨论】:

            • 由于没有提到文件的位置,我宁愿评论和询问,而不是立即回答。
            猜你喜欢
            • 2018-05-07
            • 2020-03-31
            • 1970-01-01
            • 2018-11-06
            • 2015-12-03
            • 1970-01-01
            • 1970-01-01
            • 2014-02-23
            • 2019-10-31
            相关资源
            最近更新 更多