HandlerExceptionResolver 全局异常拦截器 报错

Posted qukaige

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HandlerExceptionResolver 全局异常拦截器 报错相关的知识,希望对你有一定的参考价值。

3-Dec-2017 15:49:09.225 SEVERE [http-nio-80-exec-30] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [mvc-dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod] with root cause
	 java.lang.ClassCastException: org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler cannot be cast to org.springframework.web.method.HandlerMethod
		at interceptor.GlobalExceptionResolver.resolveException(GlobalExceptionResolver.java:25)
		at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1222)
		at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1034)
		at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)
		at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
		at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
		at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
		at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
		at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
		at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
		at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
		at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
		at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
		at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
		at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
		at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
		at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
		at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
		at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
		at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
		at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
		at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
		at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
		at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
		at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
		at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
		at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
		at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
		at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
		at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
		at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
		at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
		at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
		at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
		at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
		at java.lang.Thread.run(Thread.java:748)

报错原因: 后台的异常拦截器对handel类型转换失败造成的: 

解决办法在handel转换前进行判断:

 if(o instanceof HandlerMethod){
      String methodName = ((HandlerMethod) o).getMethod().getName();
      String className  = ((HandlerMethod)o).getBeanType().getSimpleName();
      logger.error("异常信息>>>>>ClassName:"+className+".....method:"+methodName+".....",e);
 }

  


  

以上是关于HandlerExceptionResolver 全局异常拦截器 报错的主要内容,如果未能解决你的问题,请参考以下文章

Spring 框架——利用HandlerExceptionResolver实现全局异常捕获

SpringMvc异常处理

Spring MVC统一异常处理

spring mvc 异常统一处理方式

Exception(异常处理)

全局异常处理