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

来源:互联网 发布:代替淘宝指数的软件 编辑:程序博客网 时间:2024/06/03 18:03

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 Exceptionjava.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