Caused by: java.lang.IllegalStateException: getWriter() has already been called for this response
開始的程式碼:
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();
相關文章
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解讀JavaExceptionErrorMIT
- Uncaught SyntaxError: Identifier 'Geometry' has already been declaredErrorIDE
- FeignClientSpecification‘ could not be registered. A bean with that name has already been definedclientBean
- prettier Unexpected closing tag "p". It may happen when the tag has already been closed by another tag.APP
- vue專案啟動報錯(SyntaxError: Identifier ‘__vite__injectQuery’ has already been declared (at ${mod.id}:55030:1))VueErrorIDEVite
- Caused by: java.lang.IllegalStateException: Only single-level inheritance supported: XxxxServiceJavaException
- RuntimeError: An attempt has been made to start a new process before the current process hasError
- 【HMS Core】ToolKit,Merchant Service has not been enabled yet
- Thread 1: Fatal error: init(coder:) has not been implementedthreadError
- The app.Configuration 'compile' is obsolete and has been replaced with 'implementation'APPCompile
- Transaction rolled back because it has been marked as rollback-only
- has been blocked by CORS policy跨域問題解決BloCCORS跨域
- springcloudconfig訪問gitee報錯:Authentication is required but no CredentialsProvider has been registeredSpringGCCloudGiteeUIIDE
- ViewPager內使用FragmentPagerAdapter時滑動出現 java.lang.IllegalStateException: The specified child already hViewpagerFragmentAPTJavaException
- [原創] How to pull code when shared repo commit history has been changedMIT
- How To Retry Multiple Errored Approval Workflow Processes After A Fix Or Patch Has Been ImplementedErrorAPP
- Syntax Error: ValidationError: Invalid options object. Stylus Loader has been initialized using anErrorObjectZed
- PHP Warning: exec() has been disabled for security 問題解決辦法PHP
- Error: php@8.0 has been disabled because it is a versioned formula! It was disabled on 2023-11-29.ErrorPHPORM
- BUG記錄-Sharing is only supported for boot loader classes because bootstrap classpath has been appendebootAPP
- PyTorch出現:RuntimeError: An attempt has been made to start a new process...報錯 (已解決)PyTorchError
- Android gradle問題解決: This app has been built with an incorrect configuration. Please configure your b...AndroidGradleAPPUI
- 重啟docker服務後,容器啟動報錯:Error response from daemon: id already in useDockerError
- java.lang.IllegalStateException: closedJavaException
- react-解決 fetch 跨域問題:Access to fetch at XXX from origin YYY has been blocked by CORS policyReact跨域BloCCORS
- The method getWriter() is undefined for the type HttpServletRequest的問題UndefinedHTTPServlet
- WPF TreeView SelectedItemChanged called twiceView
- python3 tkinter報錯:_tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by gridPythonErrorIDE
- Xcode證書管理出現這個錯誤:Your account already has a signing..... 的解決辦法XCode
- This request has been blocked; the content must be served over HTTPS.此請求已被阻止;內容必須透過HTTPS提供BloCHTTP
- Caused by: android.os.Android
- already opened by ClassLoader
- java.lang.IllegalStateException: Service id not legal hostnameJavaException
- Response
- onMounted is called when there is no active component 已解決
- efcore This MySqlConnection is already in useMySql
- Caused by: Error: ' ' is not a valid resource name characterError
- Caused by:.NoSuchBeanDefinitionException: No bean named 'userInfoServiceImpl' availableBeanExceptionAI