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

無愛52zj發表於2020-11-07

開始的程式碼:

                HttpServletResponse response = (HttpServletResponse) servletResponse;
                Map map = new HashMap<>();
                map.put("code", 401);
                map.put("msg", "身份認證資訊過期,請重新登陸");
                response.setContentType("application/json;charset=UTF-8");
                response.setStatus(HttpStatus.UNAUTHORIZED.value());
                PrintWriter writer = response.getWriter();
                writer.write(JSONObject.toJSONString(map));
                writer.flush();
                writer.close();

然後報錯:

Caused by: java.lang.IllegalStateException: getWriter() has already been called for this response
	at org.apache.catalina.connector.Response.getOutputStream(Response.java:550)
	at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:210)
	at javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:105)
	at org.springframework.http.server.ServletServerHttpResponse.getBody(ServletServerHttpResponse.java:84)
	at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:311)
	at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:277)
	at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:219)
	at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:123)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	... 35 common frames omitted

錯誤中說了已經使用過getWriter了
Caused by: java.lang.IllegalStateException: getWriter() has already been called for this response at org.apache.catalina.connector.Response.getOutputStream
改為以下形式就不報錯了

  ServletOutputStream outputStream = response.getOutputStream();
                outputStream.write(JSONObject.toJSONString(map).getBytes());
                outputStream.flush();
                outputStream.close();

相關文章