Cannot call sendError() after the response has been committed 错误记录

Cannot call sendError() after the response has been committed 错误记录

在使用Spring MVC开发过程中遇到了一个十分迷惑的错误 Response has been committed。

通过打断点,发现代码都正常运行通过,但是就是一直报这个错,于是仔细观察代码发现有段代码中对象引用形成了一个循环,如下代码:
	JSONObject result = new JSONObject();

    Person person = new Person();
    person.setId(1);
    person.setName("张三");

    List<Person> personList = new ArrayList<Person>();
    personList.add(person);
    person.setPersonList(personList);

    result.put("person", person);
        
后来发现,原来是通过jackson转化为Json数据的时候,发现了一个循环引用,jackson无限循环转化,最终栈溢出,导致问题出现。
而在jackson不同版本下的错误信息也不同,在1.9.4版本下错误信息为:
Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
...(以下省略)
在2.2.1版本下的错误信息为:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->com.unuse.diary.api.Person["personList"]->java.util.ArrayList[0]->com.unuse.diary.api.Person["personList"] ...(以下省略)
所以在开发过程中,要注意编写的代码,不要造成以上错误。
在此记录下。避免以后再踩坑。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值