Spring Boot项目yaml配置文件报错:YAMLException:java.nio.charset.MalformedInputException:Input length = 2

在服务器上编辑SpringBoot的application.yml文件后,由于编码格式改变导致启动时出现YAMLException,输入长度错误。解决方法是将文件重新保存为UTF-8编码格式,从而避免MalformedInputException。

问题:
一个Spring Boot项目,在服务器中用编辑器修改了application.yml配置文件,然后再次启动时报错:

org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 2

原因:修改后将文件的编码格式也改变了
解决:将文件重新保存成UTF-8的编码格式即可

<think>我们面对的问题是:在启动Java应用时出现`org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1`错误。这个错误通常是由于YAML文件(如application.yml)的编码问题引起的。根据引用[1][2][3]提供的信息,我们可以总结出以下原因和解决方案。 ### 问题原因分析 1. **YAML文件编码与系统读取编码不一致**:Spring Boot默认使用UTF-8编码读取配置文件,但如果配置文件实际保存的编码不是UTF-8(例如是GBK),则会出现编码错误[^1][^2]。 2. **IDEA编辑器编码设置问题**:在IDEA中,如果某个配置文件的编码被设置为非UTF-8(如GBK),而整个项目的默认编码是UTF-8,就会导致此问题[^2]。 3. **Maven编译时未指定编码**:如果Maven在编译时没有明确指定编码为UTF-8,可能会导致资源文件以错误编码处理[^3]。 ### 解决方案 #### 方法1:统一配置文件编码为UTF-8(推荐) **步骤:** 1. 用IDEA打开有问题的YAML文件(如`application.yml`)。 2. 检查文件右下角的编码显示(如GBK、ISO-8859-1等)。 3. 点击编码名称,选择“Convert to UTF-8”并确认转换。 4. 保存文件并重新启动应用。 #### 方法2:修改IDEA全局编码设置 **步骤:** 1. 打开IDEA设置:`File -> Settings -> Editor -> File Encodings` 2. 确保以下设置均为UTF-8: - Global Encoding: UTF-8 - Project Encoding: UTF-8 - Default encoding for properties files: UTF-8(勾选“Transparent native-to-ascii conversion”) 3. 点击Apply并重新启动IDEA。 #### 方法3:在pom.xml中强制指定编码 在Maven的pom.xml中添加以下配置,确保资源文件以UTF-8编码处理: ```xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> ``` #### 方法4:检查其他配置文件 如果错误指向的不是`application.yml`而是其他YAML文件(如`bootstrap.yml`、`logback.xml`等),同样需要检查并转换这些文件的编码为UTF-8[^2]。 ### 验证是否解决 - 重新启动应用,观察是否还有相同错误。 - 如果仍有错误,检查日志中指示的具体文件路径,确保该文件已正确转换编码。 ### 预防措施 1. **统一项目编码**:在团队开发中,确保所有成员使用相同的UTF-8编码。 2. **设置.gitattributes**:在Git仓库根目录添加`.gitattributes`文件,强制文本文件使用UTF-8: ``` *.yml text eol=lf charset=utf-8 *.properties text eol=lf charset=utf-8 ``` ### 常见误区 - 仅修改IDEA设置而不转换已有文件:必须对已有文件执行“Convert”操作。 - 忽略其他配置文件:错误可能由多个配置文件引起,需全面检查。 --- ### 错误日志分析示例 ```log org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1 ``` - **Input length = 1** 表示解析器遇到一个无法用当前字符集解码的字节[^1]。 - 通常发生在包含非UTF-8编码的中文字符时。 --- §§相关问题§§ 1. 如何解决Spring Boot读取外部配置文件时的编码问题? 2. IDEA中如何批量转换文件编码? 3. Maven资源过滤导致配置文件乱码如何处理? 4. Spring Boot多环境配置下如何管理不同编码的配置文件? [^1]: Spring Boot官方文档 - 外部化配置的编码处理规范 [^2]: IntelliJ IDEA文件编码转换操作指南 [^3]: Maven资源插件对编码的配置说明</think>### 解决 YAML 解析错误:`MalformedInputException: Input length = 1` 此错误表示 Spring Boot 在读取 YAML 配置文件时遇到**编码不匹配**问题。根本原因是 YAML 文件包含非 UTF-8 编码的字符(通常是中文注释或特殊符号),而 Spring 默认使用 UTF-8 解析配置文件。以下是系统化解决方案: --- ### 解决方案步骤 #### 1. **检查文件编码(IDEA 操作)** - 打开报错YAML 文件(如 `application.yml`) - 查看右下角编码状态(如显示 `GBK`、`ISO-8859-1` 等) - **右键点击编码区域 → `File Encoding` → 选择 `UTF-8`** - 选择 **`Convert`**(关键步骤!仅改设置不生效) ```diff ! 重要:必须选择"Convert"而非"Reload",否则仅修改设置不转换内容 ``` #### 2. **配置 Maven 编译编码(`pom.xml`)[^3]** ```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> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <encoding>UTF-8</encoding> <!-- 强制资源文件UTF-8 --> </configuration> </plugin> </plugins> </build> ``` #### 3. **检查所有配置文件** 错误可能来自: - `application.yml` / `application.properties` - `bootstrap.yml` - `logback.xml`(如引用[2]中的中文注释问题) - 其他自定义配置文件 ```bash # 检查项目中所有配置文件 find . -name "*.yml" -o -name "*.xml" -o -name "*.properties" ``` #### 4. **验证文件编码(终端命令)** ```bash # 检查文件真实编码 file -i src/main/resources/application.yml # 期望输出: # src/main/resources/application.yml: text/plain; charset=utf-8 ``` #### 5. **清理并重新构建项目** ```bash mvn clean package -DskipTests # 或 Gradle ./gradlew clean build -x test ``` --- ### 根本原因分析 1. **编码不匹配** YAML 文件包含 GBK 编码的中文字符,但 Spring 使用 UTF-8 读取[^1][^2] $$ \text{解码错误率} \propto \frac{\text{非UTF8字符数}}{\text{文件大小}} $$ 2. **构建工具未指定编码** Maven/Gradle 默认使用系统编码(如 Windows 的 GBK)处理资源文件[^3] 3. **编辑器错误处理** IDEA 可能未正确转换现有文件的编码(仅修改设置未转换内容) --- ### 预防措施 1. **项目级编码设置(IDEA)** `File → Settings → Editor → File Encodings`: - 设置 **Global Encoding** 和 **Project Encoding** 为 `UTF-8` - 勾选 **Transparent native-to-ascii conversion** 2. **添加编码检测插件(`pom.xml`)** ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>enforce-encoding</id> <phase>validate</phase> <goals><goal>enforce</goal></goals> <configuration> <rules> <requireEncoding> <encoding>UTF-8</encoding> </requireEncoding> </rules> </configuration> </execution> </executions> </plugin> ``` 3. **Git 仓库配置(`.gitattributes`)** ```gitattributes *.yml text eol=lf charset=utf-8 *.properties text eol=lf charset=utf-8 * text=auto ``` --- ### 验证是否解决 启动应用时检查日志: ```log # 成功加载配置的日志 Loaded config file 'file:.../application.yml' in 15ms ``` --- ### 特殊情况处理 若问题仅在生产环境出现: 1. 检查 Dockerfile 的编码声明: ```dockerfile ENV LANG C.UTF-8 ENV LC_ALL C.UTF-8 ``` 2. 确保部署包未受本地编码污染: ```bash unzip -p your-app.jar BOOT-INF/classes/application.yml | file - # 应输出:UTF-8 Unicode text ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值