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

Spring MVC返回ArrayList遇HttpMessageNotWritableException问题求助

解决Spring MVC Ajax返回List时的HttpMessageNotWritableException问题

我之前也碰到过一模一样的报错,咱们从几个核心点来排查和解决:

1. 确认Jackson依赖是否完整且版本匹配

这个报错最常见的原因就是缺少Jackson序列化所需的依赖,或者依赖版本和Spring版本不兼容。

如果用Maven,确保你的pom.xml里包含以下依赖(版本请和你的Spring版本对应,比如Spring 5.x对应Jackson 2.9+,Spring 6.x对应Jackson 2.12+):

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version> <!-- 替换为适配你Spring版本的版本号 -->
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.15.2</version>
</dependency>

如果是Gradle,添加:

implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.15.2'

2. 检查WipoDataUI类的序列化兼容性

Jackson需要类满足以下条件才能正常序列化:

  • 必须有无参数构造函数(如果你的类只有带参构造函数,Jackson无法实例化对象进行序列化)
  • 所有需要被序列化的字段都要有对应的getter方法(或者用@JsonProperty注解标记私有字段,让Jackson可以直接访问)

比如你的WipoDataUI应该类似这样:

public class WipoDataUI {
    private String field1;
    private Integer field2;

    // 必须有无参构造函数
    public WipoDataUI() {}

    // 带参构造函数可选
    public WipoDataUI(String field1, Integer field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    // 所有字段的getter方法
    public String getField1() {
        return field1;
    }

    public Integer getField2() {
        return field2;
    }

    // setter方法可选,除非需要反序列化
    public void setField1(String field1) {
        this.field1 = field1;
    }

    public void setField2(Integer field2) {
        this.field2 = field2;
    }
}

如果有循环引用(比如WipoDataUI里引用了另一个类,而那个类又引用了WipoDataUI),需要用@JsonIgnore忽略循环的字段,或者用@JsonManagedReference和@JsonBackReference来处理。

3. 优化Spring MVC的消息转换器配置

你的servlet.xml配置看起来没问题,但可以做一点优化:

  • 确保mvc命名空间已经正确引入到xml的根标签里:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  • 可以尝试调整转换器顺序,把Jackson的转换器放在前面:
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

4. 尝试用ResponseEntity包装返回值

有时候直接返回List可能会有类型擦除的问题,用ResponseEntity包装一下可以明确返回类型:

@RequestMapping(value = "/searchProcess", method = RequestMethod.POST )
@ResponseBody
public ResponseEntity<List<WipoDataUI>> search(@RequestParam String publicationDateFromUI, @RequestParam String publicationDateToUI) {
    List<WipoDataUI> wipodata = searchService.getData(resultData);
    return new ResponseEntity<>(wipodata, HttpStatus.OK);
}

5. 查看更详细的错误日志

你目前的日志只显示了找不到转换器,但可以开启Spring的DEBUG日志,查看MappingJackson2HttpMessageConverter的初始化过程,有没有报错信息,比如“Could not initialize Jackson2ObjectMapperFactoryBean”之类的,这能帮你定位具体是依赖问题还是配置问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:46:47