java.lang.IllegalStateException:无法从位置“classpath:/application.yml”加载属性源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49715639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
提问by Jeff Cook
I am getting below error for the Spring Cloudproject. In this project I am not doing anything special other than reading the .properties files from the GIT.
对于Spring Cloud项目,我遇到了以下错误。在这个项目中,除了从 GIT 读取 .properties 文件之外,我没有做任何特别的事情。
Please guide on what else need to be corrected out here ?
请指导这里还有什么需要纠正的?
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:535) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:494) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:462) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$null(ConfigFileApplicationListener.java:444) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at java.lang.Iterable.forEach(Unknown Source) ~[na:1.8.0_151]
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$load(ConfigFileApplicationListener.java:443) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at java.lang.Iterable.forEach(Unknown Source) ~[na:1.8.0_151]
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:440) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:331) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:213) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:196) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:183) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:169) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at pluralsight.demo.PluralsightSpringcloudM2ConfigserverGitApplication.main(PluralsightSpringcloudM2ConfigserverGitApplication.java:12) [classes/:na]
Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing a block collection
in 'reader', line 17, column 17:
- "*/perf"
^
expected <block end>, but found Key
in 'reader', line 18, column 17:
uri: https://github.com/rseroter ...
^
How we can solved the below error ? application.yml
我们如何解决以下错误? 应用程序.yml
---
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls
search-paths:
- 'station*'
repos:
perf:
pattern:
- "*/perf"
uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls-perf
search-paths:
- 'station*'
采纳答案by Vinay
Try removing quotes
尝试删除引号
---
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls
search-paths:
- 'station*'
repos:
perf:
pattern:
- */perf ##as it was trying to match the whole pattern
uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls-perf
search-paths:
- 'station*'
If still not works, please try the below
如果还是不行,请试试下面的
repos:
perf:
pattern: xx*/perf, */pref ##as the fist character can't be a wild card but it can accept multiple value.
uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls-perf
Also it is important to look for correct indentation while working with yml files.
在处理 yml 文件时寻找正确的缩进也很重要。
回答by Muji
This error indicates an issue with the formatting of the application.yml You can validate your yml file online: http://www.yamllint.com/
此错误表示 application.yml 的格式存在问题您可以在线验证您的 yml 文件:http://www.yamllint.com/
回答by Veter
I have the same problem and fix it already. If the application.yml file is well done, you can try to empty your maven repository(at Maven/repository).then do maven update at your project and run it again.
我有同样的问题并且已经解决了。如果 application.yml 文件做得好,您可以尝试清空您的 maven 存储库(在 Maven/repository)。然后在您的项目中执行 maven update 并再次运行它。
回答by DanDan
I'm not sure about the second search-paths, I think you put that in there by mistake. But for the error messages in line 17 & 18, that's an easy fix:
我不确定第二个search-paths,我认为你错误地把它放在那里。但是对于第 17 和 18 行中的错误消息,这是一个简单的解决方法:
Add another dash to the line containing the uri, so that it looks like that:
在包含 uri 的行中添加另一个破折号,使其看起来像这样:
repos:
perf:
pattern:
- */perf ##as it was trying to match the whole pattern
- uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls-perf
What comes after pattern:is supposed to be a list and you need to prefix the items with -.
后面pattern:的应该是一个列表,您需要在项目前加上-.
回答by atul sachan
I was facing the similar issue... the one of the reason is not giving proper spacing in yaml file. I have resolved as below -
我遇到了类似的问题......原因之一是没有在 yaml 文件中提供适当的间距。我已经解决如下 -
Example -
例子 -
incorrect one ->
RESERVATION:
registerHealthIndicator: true
slidingWindowSize: 100 --> this parameter have unnecessary spaces at beginning
the correct one is ->
RESERVATION:
registerHealthIndicator: true
slidingWindowSize: 100 --> correct as spaces at beginning removed
Refer below image -
参考下图 -
回答by spritus
I got that error today, and I didn't understand at first because the indentation of my conf was good. Then I realized that was because the encoding of my application.ymlwas "Microsoft something" (suggested by IntelliJ, don't remember exactly). I just copied my conf, pasted it in SublimeText, deleted my application.yml, then recreated a new file with the same name (so application.yml), copied what was in SublimeText, then pasted it in my new file.
Apparently the default encoding fixed the problem.
我今天遇到了那个错误,一开始我不明白,因为我的 conf 的缩进很好。然后我意识到这是因为我的编码application.yml是“ Microsoft something”(由 IntelliJ 建议,记不清了)。我只是复制了我的 conf,将其粘贴到 SublimeText 中,删除了 my application.yml,然后重新创建了一个具有相同名称的新文件(so application.yml),复制了 SublimeText 中的内容,然后将其粘贴到了我的新文件中。显然默认编码解决了这个问题。


![Java servlet [dispatcherServlet] 的 Servlet.service() 在路径 [] 的上下文中引发异常](/res/img/loading.gif)