You need to enable JavaScript to run this app. Spring Boot接口报JSON反序列化错误:无法从START_ARRAY转TestStepDTO
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot接口报JSON反序列化错误:无法从START_ARRAY转TestStepDTO

问题排查与修正方案

核心报错原因

从日志的异常信息可以直接定位最直接的触发点:Jackson序列化框架无法将数组格式的JSON(即标记为START_ARRAY的请求体结构),反序列化为单个TestStepDTO类型对象。
对应代码层面的问题:

  • 后端Controller通过@RequestBody TestStepDTO testStepDTO声明,要求接收单个JSON对象结构的请求体
  • 但前端AJAX请求中,将参数包裹为了数组结构:data: [{ ...单个对象的字段... }],实际发送的是仅含1个元素的JSON数组,和后端要求的参数结构完全不匹配,直接触发反序列化异常。

除了这个直接报错的问题,代码里还有两个会导致后续运行失败的隐藏坑:

1. 前端AJAX传参方式不合法

当前代码设置了contentType: 'application/json'和processData: false,但直接把原生JS对象作为data值传入。这种配置下jQuery不会自动把JS对象序列化为合法JSON字符串,就算去掉外层数组,后端依然无法正确解析参数。
正确的传参方式是手动调用JSON.stringify()把JS对象转为JSON字符串后再传入。

2. DTO类校验注解使用错误

所有字段上统一加了@NotBlank注解,但这个注解仅支持字符串类型字段,作用是校验字符串非null、非全空白字符。你的testCaseId、stepNumber是Integer类型,用@NotBlank会直接触发校验框架的类型不兼容错误。
正确的注解匹配规则:

  • 整数/自定义对象类型的非空校验,替换为@NotNull
  • 字符串类型的非空、非空白校验,保留@NotBlank

3. 后端Service初始化方式错误

你在Controller接口方法内部直接new TestStepService()创建实例,完全脱离了Spring容器的托管。这种方式下,Service内部依赖的数据库操作组件、事务管理逻辑全都是未初始化的空值,就算前面的参数解析问题全部解决,调用save()方法时一定会抛出空指针异常。
正确做法是通过依赖注入把容器托管的TestStepService实例注入到Controller中使用,不要在业务方法里手动new对象。

修正后的关键代码片段

前端AJAX传参修正

$.ajax({
    url: 'http://localhost:8080/ts/sts',
    dataType: 'json',
    type: 'post',
    contentType: 'application/json',
    // 去掉外层数组,手动序列化为合法JSON字符串
    data: JSON.stringify({ 
        "result": result[i], 
        "stepNumber": b, 
        "description":  description[i],
        "testCaseId": 1,
        "testdata": testdata[i], 
        "prerequisite": prerquisites[i]
    }),
    processData: false,
    success: function( data, textStatus, jQxhr ){
        $('#response pre').html( JSON.stringify( data ) );
    },
    error: function( jqXhr, textStatus, errorThrown ){
        console.log( errorThrown );
        console.log("testcaseid: " + testcaseid);
    }
});

DTO类注解修正

public class TestStepDTO {
    @NotNull
    private Integer testCaseId;
    @NotNull
    private Integer stepNumber;
    @NotBlank
    private String description;
    @NotBlank
    private String result;
    @NotBlank
    private String testdata;
    @NotBlank
    private String prerequisite;

    // 原有getter、setter方法保持不变
}

Controller层Service注入修正

// 原有类注解保持不变
public class TestStepController {
    // 注入Spring容器托管的Service实例,不要在方法内手动new
    @Autowired
    private TestStepService testStepService;

    @PostMapping(path = "ts/sts")
    public void saveTestStep(@RequestBody TestStepDTO testStepDTO){
        TestStep testStep = new TestStep(testStepDTO.getTestCaseId(),
                testStepDTO.getStepNumber(),
                testStepDTO.getDescription(),
                testStepDTO.getTestdata(),
                testStepDTO.getPrerequisite(),
                testStepDTO.getResult());
        testStepService.save(testStep);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.27 16:54:05