简体   繁体   English

使用PrintWriter时已为此响应调用getOutputStream()

[英]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: 编辑:

Here is the stacktrace: 这是堆栈跟踪: 在此处输入图片说明

The basics are: 基础是:

  1. headers must be written first; 标头必须先写入;
  2. then the content must be written using either getOutputStream or getWriter . 然后必须使用getOutputStreamgetWriter编写内容。

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.

相关问题 春季:已经为此响应调用了getOutputStream() - Spring: getOutputStream() has already been called for this response response.getOutputStream已被调用 - response.getOutputStream has already been called IllegalStateException:已经为此响应调用了 getOutputStream() - IllegalStateException: getOutputStream() has already been called for this response 已为此响应调用 getOutputStream() - getOutputStream() has already been called for this response 已为此响应调用HandlerInterceptor getOutputStream() - HandlerInterceptor getOutputStream() has already been called for this response JSP:已为此响应调用getOutputStream() - JSP : getOutputStream() has already been called for this response 此响应已调用“ getOutputStream()” - “getOutputStream()” has already been called for this response 已针对此响应错误调用getOutputStream() - getOutputStream() has already been called for this response error IllegalStateException:检索图像时已对此响应调用getOutputStream() - IllegalStateException: getOutputStream() has already been called for this response on retrieving image HttpsCookieFilter-IllegalStateException:此响应已调用getOutputStream() - HttpsCookieFilter - IllegalStateException: getOutputStream() has already been called for this response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM