RestAPI POST调用setter/getter传参 Jackson未识别字段报错排查
问题根因
你遇到的报错和传入TrueAge空数组没有关联,异常的核心原因是:POST请求发送后,接口返回的JSON响应数据中存在Allocation字段,但你用来接收响应的SumAll POJO类未定义该字段,Jackson做JSON反序列化时无法识别未知字段,因此抛出异常。
修复方案
- 方案1:不需要用到
Allocation字段的场景,直接在SumAll类上添加忽略未知字段的注解即可,无需修改其他逻辑:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class SumAll { // 原有类内容 }
- 方案2:业务需要用到
Allocation字段的场景,在SumAll类中补加对应属性和getter、setter方法即可,示例如下(请根据实际字段类型调整):
public class SumAll{ private int Age; private int[] TrueAge= {}; // 新增字段 private int Allocation; public int[] getTrueAge(){ return TrueAge; } public void setTrueAge(int[] TrueAge) { this.TrueAge = TrueAge; } // 新增字段的getter、setter public int getAllocation() { return Allocation; } public void setAllocation(int allocation) { Allocation = allocation; } }
其他注意事项
- 你当前贴出的
SumAll类存在语法问题:getTrueAge方法缺少闭合大括号,setTrueAge方法的赋值逻辑未加this关键字,会导致类属性无法被正确赋值,建议按上面的示例修正语法。 - 请求代码中
.as(sumAll.class)需要注意类名大小写,应该为.as(SumAll.class),否则会出现编译错误。
内容的提问来源于stack exchange,提问作者Phani
相关产品推荐
相关产品推荐

