java.nio.charset.MalformedInputException: Input length =

it2025-01-08  8

问题描述

启动SpringBoot项目失败,错误提示显示无法加载application.yml文件  

错误日志如下

2020-10-21 09:57:31,609 [restartedMain] ERROR [org.springframework.boot.SpringApplication:833] - 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:535) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:494) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:462) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$null$4(ConfigFileApplicationListener.java:444) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_181]     at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$load$5(ConfigFileApplicationListener.java:443) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_181]     at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:440) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:331) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:213) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:196) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:183) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:169) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:351) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]     at com.bonc.listname.ListNameApplication.main(ListNameApplication.java:31) [classes/:na]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]     at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.0.RELEASE.jar:2.0.0.RELEASE] Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 2     at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:254) ~[snakeyaml-1.19.jar:na]     at org.yaml.snakeyaml.reader.StreamReader.<init>(StreamReader.java:58) ~[snakeyaml-1.19.jar:na]     at org.yaml.snakeyaml.Yaml.loadAll(Yaml.java:537) ~[snakeyaml-1.19.jar:na]     at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:161) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]     at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:139) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

 

 

错误分析

  可以看出是charset.MalformedInputException错误,即字符格式错误,Input length =1还是=2都是编码不统一的问题。 产生这个错误的原因是,我们的yml文件是utf8格式的,但是在对maven项目编译时,默认使用的字符集编码是GBK格式的,因此就会造成编码错误了  

解决方法

  在项目的pom.xml文件里添加如下内容,设置编码格式即可 <properties>     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>           <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties>

 

加入以上代码即可解决错误了,顺便说下,把IDEA对项目文件的编码格式设置为UTF-8也可以避免一些不必要的编码问题,设置方式如下                          
最新回复(0)