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

Spring MVC客户端调用出现Jackson反序列化异常求助

解决Jackson反序列化错误:Can not deserialize instance of java.lang.String out of START_OBJECT token

这个问题我太熟悉了,咱们直接拆解解决:

首先看异常里的核心提示:Can not deserialize instance of java.lang.String out of START_OBJECT token,这句话的意思是:Jackson在解析JSON请求时,遇到了一个JSON对象(START_OBJECT),但你的代码里对应的字段定义却是String类型,类型完全不匹配,所以直接抛出了反序列化失败的异常。

结合你的代码和场景分析:

  • 你的Request类中,contacts字段定义的是List<String>,这意味着服务端期望接收一个字符串数组(比如["contact1", "contact2"])
  • 但客户端实际发送的是contacts对象数组(每个元素都是包含input、status、wa_id属性的JSON对象,格式类似[{"input":"xxx","status":"success","wa_id":"123"}])
  • 这种类型不匹配就是导致报错的根本原因,Postman调用正常大概率是因为你在Postman里发送的是符合List<String>格式的字符串数组,和客户端请求格式不一致。

具体修复方案

把Request类中的contacts字段类型从List<String>改成List<contacts>(顺便提一句,Java类名规范应该是大写开头,建议把contacts类改成Contacts,避免和变量名混淆),修改后的Request类代码如下:

public class Request { 
    @JsonProperty 
    private String blocking; 
    @JsonProperty 
    private String module; 
    @JsonProperty("contacts") 
    // 修改字段类型为List<contacts>
    private List<contacts> contacts; 

    public Request(){ } 

    public String getBlocking() { return blocking; } 
    public void setBlocking(String blocking) { this.blocking = blocking; } 
    public String getModule() { return module; } 
    public void setModule(String module) { this.module = module; } 
    // 同步修改getter和setter的返回值/参数类型
    public List<contacts> getContacts() { return contacts; } 
    public void setContacts(List<contacts> contacts) { this.contacts = contacts; } 

    @Override 
    public String toString() { 
        return "Request [blocking=" + blocking + ", module=" + module + ", contacts=" + contacts + "]"; 
    } 
}

修改完成后,Jackson就能正确将客户端发送的contacts对象数组反序列化为List<contacts>类型的字段了。

内容的提问来源于stack exchange,提问作者Sonal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:08:45