异常一

异常内容:

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning a simple key

导致该因此的主要原因为:application.yml文件键和值(key: value)之间要用冒号:隔开,而且冒号和值之间有一个空格,否则就报上面的错误!

错误示例:

server:
  port:8080

上述代码在por和8080之间没有空格会报错,正确处理,在它们之间添加空格。

异常二

异常信息如下:

org.yaml.snakeyaml.scanner.ScannerException: while scanning an alias in 'reader', line 5, column 18: include: *

以Spring Boot Actuator配置的配置属性为例,有以下配置:

SpringBoot基于yml配置异常情况汇总插图

在上述属性中我们看到有个值为“*”的选项。此处便会引发另外一个配置错误。

错误配置实例:

management:
  endpoints:
    web:
      exposure:
        include: *

此时“*”号并为使用单引号进行处理,启动时便会报上述异常。正确的写法应该如下:

management:
  endpoints:
    web:
      exposure:
        include: '*'

异常三

异常信息如下:

org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@’ that cannot start any token.

此问题的原因主要是在pom里面加了各个环境动态指定,如下:

<profile>
    <id>pre-env</id>
    <properties>
        <package.environment>pre</package.environment>
    </properties>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
</profile>

然后在yml中使用spring.profiles.active指定值跟上述配置不一致就会出现上面的错误。

spring:
  profiles:
    active: @environment@

异常四

异常信息如下:

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
        found character '@' that cannot start any token. (Do not use @ for indentation)
        in 'reader', line 3, column 13:active: @package.environment@
SpringBoot基于yml配置异常情况汇总插图1

某些情况下解决版本是添加如下依赖:

<dependency> 
  <groupId>org.yaml</groupId> 
  <artifactId>snakeyaml</artifactId> 
  <version>${snakeyaml.version}</version> 
</dependency> 

但并不一定使用所有类似场景。

异常五

异常内容如下:

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found char

yml配置文件,提示不能以 % 开头。解决办法,首尾加上单引号,这个与“*”的问题一致。



SpringBoot基于yml配置异常情况汇总插图2

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:http://www.choupangxia.com/2020/09/25/springboot-yml/