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: getOutputStream() has already been called for this responseJavaException
- java.lang.IllegalStateException: getOutputStream() has already been called for this response解決方案JavaException
- getOutputStream() has already been called
- 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
- java.lang.IllegalStateException: getWriteableDatabase called recursivelyJavaExceptionDatabase
- 怎麼會出現 Cannot forward after response has been committed???ForwardMIT
- 這個問題如何解決?Cannot forward after response has been committedForwardMIT
- prettier Unexpected closing tag "p". It may happen when the tag has already been closed by another tag.APP
- JIRA startup failed, JIRA has been locked.AI
- InnoDB Plugin has been upgraded to version 1.0.8Plugin
- android java.lang.IllegalStateException: trying to requery an already closed cursorAndroidJavaException
- vue專案啟動報錯(SyntaxError: Identifier ‘__vite__injectQuery’ has already been declared (at ${mod.id}:55030:1))VueErrorIDEVite
- RuntimeError: An attempt has been made to start a new process before the current process hasError
- An unexpected error has been detected by Java Runtime EnvironmentErrorJava
- 【HMS Core】ToolKit,Merchant Service has not been enabled yet
- Transaction rolled back because it has been marked as rollback-only
- Thread 1: Fatal error: init(coder:) has not been implementedthreadError
- has been blocked by CORS policy跨域問題解決BloCCORS跨域
- ORA-04068: existing state of packages has been discardedPackage
- A significant part of sql server process memory has been paged outNifiSQLServer
- Invalid configuration object webpack dev server has been initialisedObjectWebdevServer
- Caused by: java.lang.IllegalStateException: Only single-level inheritance supported: XxxxServiceJavaException
- How To Retry Multiple Errored Approval Workflow Processes After A Fix Or Patch Has Been ImplementedErrorAPP
- PHP Warning: exec() has been disabled for security 問題解決辦法PHP
- The app.Configuration 'compile' is obsolete and has been replaced with 'implementation'APPCompile
- 資料庫安裝An unexpected error has been detected by HotSpot Virtual Machine資料庫ErrorHotSpotMac
- EBS R12 - The system has not been taken off maintenance mode completely.AINaN
- /dev/root filesystem full-unable to log into server-This thread has been closeddevServerthread
- ViewPager內使用FragmentPagerAdapter時滑動出現 java.lang.IllegalStateException: The specified child already hViewpagerFragmentAPTJavaException
- [原創] How to pull code when shared repo commit history has been changedMIT
- ORA-06550 ,has been detected in fnd_global.initialize[fnd_init_sql].SQL
- springcloudconfig訪問gitee報錯:Authentication is required but no CredentialsProvider has been registeredSpringGCCloudGiteeUIIDE
- Syntax Error: ValidationError: Invalid options object. Stylus Loader has been initialized using anErrorObjectZed
- FTPS“嚴重錯誤: gnutls_handshake: A TLS fatal alert has been received.”FTPTLS
- BUG記錄-Sharing is only supported for boot loader classes because bootstrap classpath has been appendebootAPP
- Your account has been locked out. Please contact the system administrator for further assistance.