格格来闯关
113.36M · 2026-02-04
在 Java 开发 XML 中,我们经常使用 Jackson 库来处理 JSON 数据的序列化和反序列化。然而,在实际开发中,由于 JSON 和 Java 对象的字段命名风格不一致(如大小写、is 前缀等),经常会出现 UnrecognizedPropertyException 错误。本文将结合一个实际案例,分析常见的字段名不匹配问题,并提供多种解决方案,帮助开发者高效应对类似问题。
假设我们有一个 JSON 数据,结构如下:
{
"id": null,
"msg": null,
"isSuccess": true,
"code": null,
"data": {
"Total": 1700,
"Rows": [...]
}
}
对应的 Java 类 ExamListResponseVo 定义如下:
@Data
public class ExamListResponseVo {
private Object id;
private String msg;
private boolean isSuccess; // 问题1:Jackson 默认会去掉 is 前缀
private Object code;
private ExamListData data;
@Data
public static class ExamListData {
private Integer total; // 问题2:JSON 中是 "Total",大小写不匹配
private List<ExamRow> rows;
}
}
当我们使用 ObjectMapper 反序列化时,可能会遇到以下两个错误:
isSuccess 问题private boolean success; // 改为 success,与 Jackson 默认行为一致
@JsonProperty 注解@JsonProperty("isSuccess") // 强制匹配 JSON 中的 isSuccess
private boolean isSuccess;
ObjectMapper 禁用 is 前缀处理ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true); // 禁止自动去掉 is 前缀
Total 大小写问题private Integer Total; // 与 JSON 完全一致
@JsonProperty 注解@JsonProperty("Total")
private Integer total;
ObjectMapper 忽略大小写ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
@Data
@JsonIgnoreProperties(ignoreUnknown = true) // 忽略 JSON 中多余的字段
public static class ExamListData {
private Integer total;
private List<ExamRow> rows;
}
snake_case 或 camelCase。@JsonProperty 注解ObjectMapper如果项目中有大量大小写不一致的情况,可以全局配置:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
mapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true);
@JsonIgnoreProperties 避免未知字段报错ExamListResponseVo@Data
@JsonIgnoreProperties(ignoreUnknown = true) // 可选:忽略未知字段
public class ExamListResponseVo {
private Object id;
private String msg;
@JsonProperty("isSuccess") // 显式指定 JSON 字段名
private boolean isSuccess;
private Object code;
private ExamListData data;
@Data
public static class ExamListData {
@JsonProperty("Total") // 显式指定 JSON 字段名
private Integer total;
private List<ExamRow> rows;
}
}
ObjectMapper mapper = new ObjectMapper();
// 可选:全局配置
// mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
String responseBody = "..."; // JSON 字符串
ExamListResponseVo result = mapper.readValue(responseBody, ExamListResponseVo.class);
| 问题 | 解决方案 | 适用场景 |
|---|---|---|
isSuccess 不匹配 | 修改字段名 / @JsonProperty | 布尔字段带 is 前缀 |
Total 大小写问题 | 修改字段名 / @JsonProperty / 全局配置 | JSON 字段名大小写不一致 |
| 未知字段报错 | @JsonIgnoreProperties | 第三方 API 返回不可控 JSON |
推荐做法:
尽量统一 JSON 和 Java 字段名(如 camelCase)。
必要时使用 @JsonProperty 显式映射。
全局配置 ObjectMapper 适用于大型项目。
使用 @JsonIgnoreProperties 增强鲁棒性。
通过本文的解决方案,你可以轻松应对 Jackson 反序列化时的字段名不匹配问题,提高开发效率!