码上敲享录 > java常见报错解答 > java.lang.IllegalStateException: getWriter() has already been called for this response

java.lang.IllegalStateException: getWriter() has already been called for this response

上一章章节目录下一章 2020-04-21已有348人阅读 评论(0)

java.lang.IllegalStateException: getWriter() has already been called for this response


解决方法:

以下代码就会报这种错误,已经使用PrintWriter 打印到浏览器了,还调用return 。

@RequestMapping("$api/design")

   public Designer index(String moodTxt, HttpServletRequest request, HttpServletResponse response) {

       Designer designer = GetUserInfo.currentUserInfo();

       String callback = request.getParameter("callback");

       if (!StringUtils.isEmpty(callback)) {//JSONP

           response.setContentType("application/json;charset=UTF-8");

           try {

               response.setHeader("Pragma", "No-cache");

               response.setHeader("Cache-Control", "no-cache");

               response.setCharacterEncoding("UTF-8");

               PrintWriter out = response.getWriter();

               out.print(callback + "(" + JSON.toJSONString(designer) + ")");

               out.flush();

               out.close();

               return null;

           } catch (Exception e) {

               e.printStackTrace();

           }


       }

       return designer;

   }


可以整理代码逻辑加一些判断,或者在最后使用return null;


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
向大家推荐《ireport实战教程》:https://xiaozhuanlan.com/ireport?rel=6923152725
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交