源代码:

@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

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

  1. 报错:java.lang.IllegalStateException: Cannot call sendError() after the response has been committed(待解答)

    严重: Servlet.service() for servlet [default] in context with path [/20161101-struts2-1] threw excepti ...

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

    http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...

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

    1.问题描述 严重: Servlet.service() for servlet [default] in contextwith path [/OxygenCloud] threw exceptio ...

  4. Java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

    在使用response重定向的时候,报以下错误:Java.lang.IllegalStateException: Cannot call sendRedirect() after the respon ...

  5. struts2异常记录--java.lang.IllegalStateException

    java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFa ...

  6. java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade

    2012-10-4 19:50:37 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for se ...

  7. myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).

    把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...

  8. java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...

  9. java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

    java.lang.IllegalStateException: Not allowed to create transaction on sharedEntityManager - use Spri ...

随机推荐

  1. (4)activiti工作流引擎之uel表达式

    有了前面几章,我们肯定有一定的困惑,activiti如何与实际业务整合,比如一条采购单,如何跟一个流程实例互相关联起来? 这里就需要使用到activiti启动流程实例时设置一个流程实例的busines ...

  2. js中创建table表格

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. VS Code设置中文和配置Python环境

    前言: Visual Studio Code(以下简称VSCode)是一个轻量且强大的代码编辑器,支持Windows,OS X和Linux.内置JavaScript.TypeScript和Node.j ...

  4. jquery 替换节点实例

    描述: 要求用户选择一个自己不喜欢的商品 说明: 左边是商品列表,右面显示用户不喜欢的商品. 首先用选择器获得用户点击的元素,然后替换右面用户选择的不喜欢的商品. <!doctype html& ...

  5. springboot中的任务(异步任务--定时任务--邮件任务)

    1.pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  6. Qt's Undo Framework

    Overview of Qt's Undo Framework Introduction Qt's Undo Framework is an implementation of the Command ...

  7. FasterRCNN目标检测实践纪实

    首先声明参考博客:https://blog.csdn.net/beyond_xnsx/article/details/79771690?tdsourcetag=s_pcqq_aiomsg 实践过程主线 ...

  8. Python中ndarray数组切片问题a[-n -x:-y]

    先看看如下代码: >>a=np.arange(10)>>a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>a[-7:] array( ...

  9. 测试用数据库表设计和SessionFactory

    本篇为struts-2.5.2和spring-3.2.0以及hibernate-4.2.21的整合开篇. 一.测试的数据库表. 用户.角色和权限关系表.数据库是Mysql5.6.为了考虑到一些特殊数据 ...

  10. zstu4273 玩具 2017-03-22 14:18 49人阅读 评论(0) 收藏

    4273: 玩具 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 700  Solved: 129 Description 商店有n个玩具,第i个玩具有 ...