java.lang.IllegalStateException:无法从位置加载属性源' classpath:/application.yml'

时间:2018-04-08 07:46:00

标签: java spring-boot

我在 Spring Cloud 项目中遇到以下错误。在这个项目中,除了从GIT读取.properties文件之外,我没有做任何特别的事情。

请指导此处需要纠正的其他内容?

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$4(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$5(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 ... 
                    ^

我们如何解决以下错误? 的 application.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*'

7 个答案:

答案 0 :(得分:7)

此错误表示应用程序的格式存在问题。 您可以在线验证yml文件: http://www.yamllint.com/

答案 1 :(得分:1)

尝试删除引号

---
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*'

如果仍然无效,请尝试以下

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

在使用yml文件时,查找正确的缩进也很重要。

答案 2 :(得分:1)

我今天遇到了这个错误,起初我并不理解,因为我的conf缩进效果很好。然后我意识到那是因为我的application.yml的编码是“ Microsoft something ”(由IntelliJ建议,不记得确切了)。我只复制了conf,粘贴到SublimeText中,删除了application.yml,然后重新创建了一个同名的新文件(所以 private void ponaredek_MouseDown(object sender, MouseButtonEventArgs e) { mouseDown = true; //x1 = System.Windows.Forms.Control.MousePosition; x1 = new System.Drawing.Point((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y); } ),复制了SublimeText中的内容,然后粘贴到了我的新文件中。 显然,默认编码解决了该问题。

答案 3 :(得分:0)

我有相同的问题,已经解决。如果application.yml文件做得很好,您可以尝试清空Maven存储库(位于Maven /存储库)。然后在项目中进行maven更新,然后再次运行。

答案 4 :(得分:0)

我不确定第二个search-paths,我想您误把它放在了那里。但是对于第17和18行中的错误消息,这很容易解决:

在包含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

pattern:之后的内容应该是列表,您需要在项目前加上-

答案 5 :(得分:0)

我正面临类似的问题...原因之一是yaml文件中的间距不正确。我已解决如下-

示例-

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

请参考下图-

enter image description here

答案 6 :(得分:0)

如果其他答案没有帮助,并且您像我一样使用 IntelliJ,请尝试重新加载 Gradle 项目,该项目已为我修复。