java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读

源代码:

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    String uri = request.getRequestURI();
    if(pathMatcher.match("/", uri)) {
        System.err.println("跳转");
        response.sendRedirect("/swagger-ui.html");
        // return false;    // 如果此处不返回false, 则springMvc会继续对“/”路径进行处理,就会出现多次返回响应的错误。

    }
    return true;
}

注:此处对“/”路径的访问返回404.

DispatcherServlet.doDispatch()中对拦截 器的preHandle进行调用:

// 如果拦截器的PreHandle返回false,则此处直接返回退出方法。
if(!mappedHandler.applyPreHandle(request,response)){
    return ;
}
// Actually invoke the handler. 调用处理器
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// 由此处可知如果拦截器的preHandle方法返回false则不会调用处理器(控制器类的方法)

mappedHandler是一个HandlerExcutionChain对象由HandlerMapping返回,HandlerExcutionChain包含一个Handler(处理器对象)和拦截器数组,通过applyPreHandle(request,response)方法会对拦截器数组中的每一个拦截器的preHandle进行调用。

// HandlerExcutionChain类
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HandlerInterceptor[] interceptors = getInterceptors();
    if (!ObjectUtils.isEmpty(interceptors)) {
        for (int i = 0; i < interceptors.length; i++) {
            HandlerInterceptor interceptor = interceptors[i];
            if (!interceptor.preHandle(request, response, this.handler)) {
                triggerAfterCompletion(request, response, null);
                return false;
            }
            this.interceptorIndex = i;
        }
    }
    return true;
}

在本例中出现上述错误的原因:

  1. 拦截器拦截了对“/”路径的请求,并且调用response.sendRedirect("/swagger-ui.html")返回了响应。由于拦截器没有返回false,所以SpringMVC会继续对“/”路径进行处理。
  2. 在没有找到“/”对应的处理器时,SpringMVC默认会使用ResourceHttpRequestHandler进行请求处理。ResourceHttpRequestHandler在进行请求处理时会进行404检查,如果路径或资源不存在则会调用response.sendError(HttpServletResponse.SC_NOT_FOUND);源码如下:
    // ResourceHttpRequestHandler中进行404检查
    // For very general mappings (e.g. "/") we need to check 404 first
    Resource resource = getResource(request);
    if (resource == null) {
        logger.debug("Resource not found");
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
  3. 由2可知,如果资源不存在就会调用response.sendError(HttpServletResponse.SC_NOT_FOUND);而在拦截器中已经调用response.sendRedirect("/swagger-ui.html")对响应进行了返回,所以就会出现多次返回响应的错误。

对于上述问题的解决办法是在response.sendRedirect("/swagger-ui.html");后返回false,或者将拦截路径由“/”改为response.sendError(HttpServletResponse.SC_NOT_FOUND);后的路径(在此处为“/error”)。

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    String uri = request.getRequestURI();
    if(pathMatcher.match("/", uri)) {

        response.sendRedirect("/swagger-ui.html");
        return false;    // 如果此处不返回false, 则springMvc会继续对“/”路径进行处理,就会出现多次返回响应的错误。

    }
    return true;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    String uri = request.getRequestURI();
    if(pathMatcher.match("/error", uri)) {

        response.sendRedirect("/swagger-ui.html");

    }
    return true;
}

下面是SpringMVC处理"/api/test/error"请求时打印的部分日志(/api/test/error没有对应的处理器,即该路径不存在,报404错误):

注:自定义的拦截器是在映射结束后才执行的。

// 请求路径
2019-02-20 14:55:57.086 DEBUG 2676 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/api/test/error", parameters={}
// 处理器映射器和请求路径对应的处理器
2019-02-20 14:55:57.092 DEBUG 2676 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
// 处理结果
2019-02-20 14:55:57.094 DEBUG 2676 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2019-02-20 14:55:57.095 DEBUG 2676 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND

下面是SpringMVC处理“/api/test/error1”请求时打印的部分日志(/api/test/error1对应的处理器在org.lwt.controller.RoleController类下的.ErrorTest()方法):

// 请求路径
2019-02-20 15:13:23.860 DEBUG 2676 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : GET "/api/test/error1", parameters={}
// 处理器映射器和请求路径对应的处理器
2019-02-20 15:13:23.861 DEBUG 2676 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.lwt.vo.Result<java.lang.String> org.lwt.controller.RoleController.ErrorTest()

拦截器调用
// 处理结果
2019-02-20 15:13:23.865 DEBUG 2676 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.joda.time.IllegalInstantException: 自己抛出错误

2019-02-20 15:13:23.867 ERROR 2676 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.joda.time.IllegalInstantException: 自己抛出错误] with root cause
// RoleController类
@RestController
@RequestMapping("/api")
@Api
public class RoleController {
    // /api/test/error1对应的处理器
    @GetMapping("/test/error1")
    public Result<String> ErrorTest(){
        throw new IllegalInstantException("自己抛出错误");
        // return Result.success("多参数传递");
    }
}

下面是SpringMVC处理“/api/test/error1”请求时打印的部分日志(/api/test/error1对应的处理器在org.lwt.controller.RoleController类下的.ErrorTest()方法):

此处和上一次调用的区别是此次调用处理器没有报错:

// 请求路径
2019-02-20 15:21:31.440 DEBUG 8252 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/api/test/error1", parameters={}
// 处理器映射器和请求路径对应的处理器
2019-02-20 15:21:31.444 DEBUG 8252 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.lwt.vo.Result<java.lang.String> org.lwt.controller.RoleController.ErrorTest()

拦截器中的uri: /api/test/error1

// 处理结果
2019-02-20 15:21:31.473 DEBUG 8252 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using ‘application/json;q=0.8‘, given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]

2019-02-20 15:21:31.473 DEBUG 8252 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing [org.lwt.vo.Result@334348d5]

2019-02-20 15:21:31.486 DEBUG 8252 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

原文地址:https://www.cnblogs.com/yourblog/p/10407056.html

时间: 2024-11-09 00:55:00

java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读的相关文章

java.lang.IllegalStateException: Cannot call sendError() after the response has been committe

1.问题描述 严重: Servlet.service() for servlet [default] in contextwith path [/OxygenCloud] threw exception java.lang.IllegalStateException:Cannot call sendError() after the response has been committed 2.问题分析 从提示错误的字面意思判断“Cannotcall sendError() after the r

WARN::Committed before 500 Unable to show problem report:|java.lang.IllegalStateException

2015-01-06 18:23:17.160:WARN::Committed before 500 Unable to show problem report:|java.lang.IllegalStateException: STREAM||Class: org.mortbay.jetty.Response|File: Response.java|Method: getWriter|Line: 616 - org/mortbay/jetty/Response.java:616:-1 2015

java.lang.IllegalStateException错误

严重: Servlet.service() for servlet default threw exceptionjava.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407) at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:852) at

java.lang.IllegalStateException: Failed to load ApplicationContext

1.错误描述 INFO:2015-02-05 22:14:21[main] - Loading XML bean definitions from class path resource [applicationContext.xml] INFO:2015-02-05 22:14:22[main] - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning INFO:2015-02-05

java.lang.IllegalStateException: ContainerBase.addChild: start

java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina] Tomcat部署Servlet时出错 caused by: Caused by: java.lang.IllegalArgumentException: Invalid <url-patt

开发中遇到的问题(一)——java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

1.错误描述: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 2.起因: 在Manifest中设置我的activity全屏 1 <activity android:name=".SplashActivity" 2 android:theme="@android:style/Theme.Black.No

java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already.

java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.xml.bind.JAXBContext]. 解决方法:先将项目remove,然后停止服务器,到应用程序目录下将部署的应用删除,然后启动服务器,重新部署就ok了 原因:暂不明确,可能是修改后有原来的残留物.

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

java.lang.IllegalStateException: getWriter() has already been called for this response 出现原因: 1.代码中有打开的Response.getWriter(),未关闭 2.再次使用了ServletOutputStream out = response.getOutputStream(); 通过查看代码,response中的usingWriter=true,想办法将该标志位设置为false. response.r

Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError

SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.Exception in thread "main" java.lang.Exception