Cannot construct instance of `***.***.ArrayList` (although at least one Creator ...

2026年08月25日 15:29 30.17 秒 success

错误信息

status_code=400, JSON parse error: Cannot construct instance of `***.***.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('</block>'); nested exception is ***.***.***.***.***.MismatchedInputException: Cannot construct instance of `***.***.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('</block>') at [Source: (***.***.***.StreamUtils$NonClosingInputStream); line: 1, column: 104892] (through reference chain: ***.***.***.***.***.***.***.ChatCompletionRequest["stop"])

AI 解决方案

🔍 报错分析

1. 🌐 错误翻译


原文:status_code=400, JSON parse error: Cannot construct instance of `***.***.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('</block>'); nested exception is ***.***.***.***.***.***.MismatchedInputException: Cannot construct instance of `***.***.ArrayList` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('</block>') at [Source: (***.***.***.StreamUtils$NonClosingInputStream); line: 1, column: 104892] (through reference chain: ***.***.***.***.***.***.***.ChatCompletionRequest["stop"])
译文:状态码=400,JSON解析错误:无法构造`***.***.ArrayList`的实例(尽管至少存在一个Creator):没有接受String参数的构造函数/工厂方法来从String值('</block>')反序列化;嵌套异常为***.***.***.***.***.***.MismatchedInputException:无法构造`***.***.ArrayList`的实例(尽管至少存在一个Creator):没有接受String参数的构造函数/工厂方法来从String值('</block>')反序列化,位于[源:(***.***.***.StreamUtils$NonClosingInputStream);行:1,列:104892](通过引用链:***.***.***.***.***.***.***.ChatCompletionRequest["stop"])

2. 🎯 原因分析


根本原因:JSON反序列化时类型不匹配。服务端期望stop字段是一个ArrayList集合类型,但实际接收到的是字符串值</block>,Jackson无法将字符串直接转换为目标集合类型。

具体位置:错误发生在解析ChatCompletionRequest对象的stop字段时,JSON数据中该位置是字符串,而目标类型是ArrayList

3. 💡 解决方案


需要检查JSON请求数据和对应的Java类定义,确保类型匹配。以下提供两种常见修复方案:

方案一:修正JSON请求数据(如果stop本应是集合)

{
  "stop": ["</block>"],
  // 其他字段...
}

将字符串包装成数组。

方案二:修改Java类定义(如果stop本应是字符串)

// 错误定义:stop是ArrayList
private ArrayList<String> stop;

// 修正为:stop是String
private String stop;

方案三:若需兼容两种类型,添加@JsonFormat注解

import com.fasterxml.jackson.annotation.JsonFormat;

public class ChatCompletionRequest {
    @JsonFormat(shape = JsonFormat.Shape.ARRAY)
    private ArrayList<String> stop;
    
    // getter/setter等
}

4. 🛡️ 预防措施


  • 前后端数据契约明确:接口文档需明确每个字段的数据类型,避免类型理解歧义

  • 使用类型安全的验证工具:集成Swagger/OpenAPI自动校验请求参数类型,或使用Postman的Schema验证功能

  • 统一序列化/反序列化配置:确保项目全局使用统一的Jackson配置,避免因注解不一致导致的类型转换问题

  • 单元测试覆盖边界:为关键的数据转换逻辑编写单元测试,覆盖字符串、集合等不同类型输入的场景