Cannot call sendError() after the response has been committed 错误记录
程序员文章站
2022-03-29 17:55:11
...
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 (*Error) (through reference chain: java.util.ArrayList[0]->com.unuse.diary.api.Person["personList"]->java.util.ArrayList[0]->com.unuse.diary.api.Person["personList"] ...(以下省略)
所以在开发过程中,要注意编写的代码,不要造成以上错误。
在此记录下。避免以后再踩坑。
推荐阅读
-
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读
-
java.lang.IllegalStateException: Cannot forward after response has been committed
-
Cannot call reset() after response has been committed
-
Hibernate项目报错:Cannot call sendError() after the response has been committed
-
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读
-
Cannot forward after response has been committed问题分析及解决
-
Hibernate项目报错:Cannot call sendError() after the response has been committed
-
Cannot call sendError() after the response has been committed 错误记录
-
Cannot call sendError() after the response has been committed
-
错误提示:Cannot call sendError() after the response has been committed的解决方法