19

I have a very simple Spring Boot app and I have application.properties, and now I want to move to application.yml

This is the application.yml file:

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

So I've deleted application.properties file and I've written the application.yml file. If I run the app I get this exception:

   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

I think I need to make other modification to my code to use this application.yml file but I don't know what to do?

This is 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>

Any feedback will be appreciated. Thank you!

10 Answers10

39

I built an online conversion tool (https://env.simplestep.ca/) that converts between spring boot yaml, properties and environment variables--using spring boot's relaxed binding rules

enter image description here

sdoxsee
  • 4,451
  • 1
  • 25
  • 60
4

for intellij-IDEA : go to file > settings > plugins then search for a plugin called: Properties to YAML Converter. Install it and After that restart your IDE then right click the application.properties file and you will get an option called "convert to yaml"

3

Check if you are using tabs for indentation. This is not permitted by the YAML spec:

To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently. Note that most modern editors may be configured so that pressing the tab key results in the insertion of an appropriate number of spaces.

manhvd
  • 308
  • 2
  • 6
3

You can use the command line tool props2yaml for generic automatic (and correct) translation from properties to yaml.

vboerchers
  • 767
  • 1
  • 6
  • 16
3

Found a good online tool to convert properties to YAML -

Properties to YAML online converter

enter image description here

Batman Rises
  • 597
  • 6
  • 9
1

If you use Eclipse or STS it's very simple.

Just open the context menu on the .properties file and choose "Convert to Yaml file".

The file will be converted automatically. If there is not this feature on your context menu you need to install Yaml plugin from

Help menu-> Eclipse market place.
Shadyar
  • 709
  • 8
  • 16
0

I think your datasource.url value needs to be in quotes, due to the ':'. Try this:

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

Also, as was pointed about above, you should use spaces for indentation; typically, two spaces per level.

Good luck!

Jamie Bisotti
  • 2,605
  • 19
  • 23
0

This is just an additional answer, might help other developers.

I found a better and easy tool to convert properties file to YML file. convert from properties to yml

You have to upload your .properties file and then Choose To YML file option and it will generate a pretty good yml file. This tool supports many other formats also like Yml to XML, Yml to Properties, CSV, JSON, etc.

bharatpatel
  • 1,203
  • 11
  • 22
  • Unfortunately, it removes all comments and doesn't properly convert indexed properties (`my.property[0]=x`, `my.property[1]=y`, etc.) – Paulo Merson May 05 '23 at 19:18
0

File application.yml must be in the root folder resources.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Andrey
  • 1
  • 2
    Since the location of the file wasn't mentioned, I'd rather comment and ask instead of answering immediately. – TimSch Sep 08 '21 at 12:13
-2

Just for others looking a similar answer, in a Spring project running in IntelliJ, it works just change the extension of the application.properties to application.yml. In my case the file was empty, so I didn't have to change anything and I could use application.yml propertly.

Alex
  • 428
  • 5
  • 8