Home > Java > javaTutorial > How to solve the error when springboot reads application.yml

How to solve the error when springboot reads application.yml

PHPz
Release: 2023-05-22 11:49:05
forward
1997 people have browsed it

springboot reported an error when reading application.yml

When the springboot project started, an error occurred while reading the configuration file

The error message is as follows

Failed to load property source from location 'classpath:/application.yml'
......
org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1

After searching for information, the reason was basically determined to be a problem with the file encoding format (the original yml file was in GBK format), so I changed the GBK format to UTF-8. After the change, I found that the Chinese comments in the previous file were garbled. The cause of the problem should be It comes from here, so I deleted all the comments and then started it normally.

Note: After the file format is changed, check whether there are garbled characters and comments will also be affected.

@Value reads Application.yml as null

Project requirements

There is a hard-coded url in springBoot and sends an http request to obtain a video address.

How to solve the error when springboot reads application.yml

The initial idea was like this, and then I considered that writing this way was not flexible enough. Because it is a springBoot project, it is very convenient to get the url directly in the configuration file.

How to solve the error when springboot reads application.yml

Then the enemy who needs to get the Value uses the @VALUE annotation to get

How to solve the error when springboot reads application.yml

but the life and death cannot be obtained there. The URL is always empty.

Solution

Finally I found that I had modified this variable with static before. Static initializes variables before the class is loaded. So @VALUE does not take effect at this time. So for NULL

, you only need to remove static.

How to solve the error when springboot reads application.yml

The above is the detailed content of How to solve the error when springboot reads application.yml. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template