You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON转Java对象时出现UnrecognizedPropertyException问题求助

问题:Jackson反序列化抛出UnrecognizedPropertyException(isPrimary字段未识别)

问题场景

将指定JSON转换为Java对象时,出现UnrecognizedPropertyException,提示JSON中的"isPrimary"字段未被识别,Jackson仅识别colors、primary、rgb三个属性。

JSON内容

{
"colors": ["red", "green", "blue"],
"isPrimary": true,
"rgb": {
"r": 255,
"g": 0,
"b": 0
}
}

Color实体类代码

public class Color {
    private List<String> colors;
    private boolean isPrimary;
    private HashMap<String,Integer> rgb = new HashMap<String,Integer>();


    public Color(){};
    public List<String> getColors() {
        return colors;
    }

    public void setColors(List<String> colors) {
        this.colors = colors;
    }

    public boolean isPrimary() {
        return isPrimary;
    }

    public void setPrimary(boolean primary) {
        isPrimary = primary;
    }

    public HashMap<String, Integer> getRgb() {
        return rgb;
    }

    public void setRgb(HashMap<String, Integer> rgb) {
        this.rgb = rgb;
    }

    @Override
    public String toString() {
        return "Color{" +
                "colors=" + colors +
                ", isPrimary=" + isPrimary +
                ", rgb=" + rgb +
                '}';
    }
}

主类代码

public static void main(String[] args) throws JsonProcessingException {
        String jsonStr = "{\r\n" +
                "  \"colors\": [\"red\", \"green\", \"blue\"],\r\n" +
                "  \"isPrimary\": true,\r\n" +
                "  \"rgb\": {\r\n" +
                "    \"r\": 255,\r\n" +
                "    \"g\": 0,\r\n" +
                "    \"b\": 0\r\n" +
                "  }\r\n" +
                "}";
        ObjectMapper objectMapper = new ObjectMapper();
        Color color = objectMapper.readValue(jsonStr, Color.class);
        System.out.println(color);
    }

异常信息

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isPrimary" (class itstep.task_5.Color), not marked as ignorable (3 known properties: "colors", "primary", "rgb"])
 at [Source: (String)"{
  "colors": ["red", "green", "blue"],
  "isPrimary": true,
  "rgb": {
    "r": 255,
    "g": 0,
    "b": 0
  }
}";

问题原因

Jackson默认命名规则对布尔类型属性的解析逻辑:isXxx()格式的getter方法会被识别为对应属性xxx,而非isXxx。你的实体类中isPrimary()方法被Jackson映射为属性primary,但JSON中的字段是isPrimary,两者命名不匹配,导致反序列化失败。


解决方法

方案1:调整实体类字段与方法命名,匹配JSON字段

修改实体类的字段名和setter方法,让Jackson的默认规则能匹配JSON的isPrimary字段:

public class Color {
    private List<String> colors;
    // 字段名保持isPrimary
    private boolean isPrimary;
    private HashMap<String,Integer> rgb = new HashMap<String,Integer>();

    public Color(){};

    // 省略其他getter/setter

    public boolean isPrimary() {
        return isPrimary;
    }

    // 修改setter方法名,与字段名对应
    public void setIsPrimary(boolean isPrimary) {
        this.isPrimary = isPrimary;
    }

    // 其余代码不变
}

方案2:使用@JsonProperty注解强制指定映射关系

在实体类的字段或getter方法上添加@JsonProperty注解,明确指定该属性对应JSON的isPrimary字段,无需修改方法命名:

public class Color {
    private List<String> colors;
    // 在字段上添加注解
    @JsonProperty("isPrimary")
    private boolean isPrimary;
    private HashMap<String,Integer> rgb = new HashMap<String,Integer>();

    public Color(){};

    // 或者在getter方法上添加注解
    @JsonProperty("isPrimary")
    public boolean isPrimary() {
        return isPrimary;
    }

    // 原有setter方法可保持不变
    public void setPrimary(boolean primary) {
        isPrimary = primary;
    }

    // 其余代码不变
}

方案3:配置ObjectMapper忽略未知字段(兜底方案)

如果不想修改实体类,可配置ObjectMapper忽略所有未识别的字段,但该方案会跳过所有匹配失败的字段,可能隐藏其他映射问题,仅建议作为临时兜底:

ObjectMapper objectMapper = new ObjectMapper();
// 关闭"未知字段抛出异常"的特性
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Color color = objectMapper.readValue(jsonStr, Color.class);

内容的提问来源于stack exchange,提问作者Максим

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 20:15:37