[英]getOutputStream() has already been called for this response when using PrintWriter
I'm working on this Java Sprint 3.0 application where I'm passing data to a dataTable. 我正在此Java Sprint 3.0应用程序上工作,在该应用程序中我正在将数据传递到dataTable。 Everything works fine, but every so often I see this error:
一切正常,但我经常看到此错误:
ERROR [[dispatcher]] Servlet.service() for servlet dispatcher threw exception
java.lang.IllegalStateException: getOutputStream() has already been called
for this response. 对于这个回应。
Here is my code: 这是我的代码:
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping(value = "/dataTable", method = RequestMethod.GET)
public void serverSide(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-store");
PrintWriter out = response.getWriter();
out.print(dataTableService.viewUsers(request));
}
I have tried adding the following: 我尝试添加以下内容:
out.flush();
out.close();
return;
I have also tried using response.getOutputStream().print(dataTableService.viewUsers(request)) and response.getWriter().append(dataTableService.viewUsers(request)) instead of using a PrintWriter but nothing seems to fix it. 我也尝试过使用response.getOutputStream()。print(dataTableService.viewUsers(request))和response.getWriter()。append(dataTableService.viewUsers(request))而不是使用PrintWriter,但是似乎没有任何解决方法。
EDIT: 编辑:
The basics are: 基础是:
getOutputStream
or getWriter
. getOutputStream
或getWriter
编写内容。 What still can go wrong: 仍然会出问题的地方:
Basic errors like using both response.getOutputStream()
and response.getWriter()
- very unlikely here. 基本错误,例如同时使用
response.getOutputStream()
和response.getWriter()
-在这里很少发生。
Control flow: 控制流:
if (...) { ... redirect // Missing return } ... normal output
A filter or interceptor: ordinarily unlikely; 过滤器或拦截器:通常不太可能; though here are annotations.
虽然这里是注释。
out.flush(); // OKAY // Probably NOT OKAY: out.close();
Servlet fields being used. 正在使用Servlet 字段 。 The service methods should be stateless in themselves.
服务方法本身应该是无状态的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.