13.3、异常处理——DefaultHandlerExceptionResolver
来源:互联网 发布:移动互联网和大数据 编辑:程序博客网 时间:2024/06/07 03:40
当程序发生异常时,DefaultHandlerExceptionResolver异常解析器默认可以解析指定的异常,见下面的源码,默认可以解析HttpRequestMethodNotSupportedException、HttpMediaTypeNotSupportedException等异常类,当有这些类的异常发生时,默认交给DefaultHandlerExceptionResolver异常解析器进行解析,DispatcherServlet默认实现了DispatcherServlet 的Bean。
DefaultHandlerExceptionResolver源码
@Override @Nullable protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) { try { if (ex instanceof HttpRequestMethodNotSupportedException) { return handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException) ex, request, response, handler); } else if (ex instanceof HttpMediaTypeNotSupportedException) { return handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException) ex, request, response, handler); } else if (ex instanceof HttpMediaTypeNotAcceptableException) { return handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException) ex, request, response, handler); } else if (ex instanceof MissingPathVariableException) { return handleMissingPathVariable((MissingPathVariableException) ex, request, response, handler); } else if (ex instanceof MissingServletRequestParameterException) { return handleMissingServletRequestParameter((MissingServletRequestParameterException) ex, request, response, handler); } else if (ex instanceof ServletRequestBindingException) { return handleServletRequestBindingException((ServletRequestBindingException) ex, request, response, handler); } else if (ex instanceof ConversionNotSupportedException) { return handleConversionNotSupported((ConversionNotSupportedException) ex, request, response, handler); } else if (ex instanceof TypeMismatchException) { return handleTypeMismatch((TypeMismatchException) ex, request, response, handler); } else if (ex instanceof HttpMessageNotReadableException) { return handleHttpMessageNotReadable((HttpMessageNotReadableException) ex, request, response, handler); } else if (ex instanceof HttpMessageNotWritableException) { return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler); } else if (ex instanceof MethodArgumentNotValidException) { return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response, handler); } else if (ex instanceof MissingServletRequestPartException) { return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request, response, handler); } else if (ex instanceof BindException) { return handleBindException((BindException) ex, request, response, handler); } else if (ex instanceof NoHandlerFoundException) { return handleNoHandlerFoundException((NoHandlerFoundException) ex, request, response, handler); } else if (ex instanceof AsyncRequestTimeoutException) { return handleAsyncRequestTimeoutException( (AsyncRequestTimeoutException) ex, request, response, handler); } } catch (Exception handlerException) { if (logger.isWarnEnabled()) { logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException); } } return null; }
下面以HttpRequestMethodNotSupportedException为例来测试,当发生HttpRequestMethodNotSupportedException异常时,会有DefaultHandlerExceptionResolver异常解析器进行处理。
1、web.xml和spring的配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
spring的配置文件springmvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.lzj.springmvc"></context:component-scan> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven></beans>
2、创建请求
在WebContent下面创建index.jsp,内容为:
<a href="springMVC/testDefaultHandlerExceptionResolver">Test DefaultHandlerExceptionResolver</a>
3、创建请求控制器
控制器用于截获index.jsp中的请求
@Controller@RequestMapping("/springMVC")public class TestSpringMVC { //设置默认接收POST请求 @RequestMapping(value="/testDefaultHandlerExceptionResolver", method=RequestMethod.POST) public String testDefaultHandlerExceptionResolver(){ System.out.println("testDefaultHandlerExceptionResolver"); return "success"; }}
当发送Index.jsp请求时,由于index.jsp中的超链接请求为GET请求,当控制器截获该请求后,由于控制器设置的默认接收POST请求,因此会抛出一个HttpRequestMethodNotSupportedException异常,该异常默认由DefaultHandlerExceptionResolver解析器进行解析,返回个客户端异常界面,如下图所示:
浏览器中显示不支持GET请求。
- 13.3、异常处理——DefaultHandlerExceptionResolver
- springMVC—异常处理
- Javase—异常处理
- JAVA——异常及异常处理
- php异常处理—多个异常
- php异常处理—重新抛出异常
- 详解Java异常处理——编程式异常处理
- 详解Java异常处理——自动异常处理
- java异常处理三——怎么处理异常
- struts2——处理异常!!!
- 异常处理——python
- JAVA——异常处理
- as3——异常处理
- Java——异常处理
- Java——异常处理
- springmvc——异常处理
- Java——异常处理
- 异常处理——三角形
- day-13
- 比特币区块结构Merkle树及简单支付验证分析
- Json Web Token简介
- shtml的一个小问题
- poj3273 二分 易错题
- 13.3、异常处理——DefaultHandlerExceptionResolver
- 使用python将csv文件数据可视化
- java语言程序设计 第十三章 (13.17、13.18)
- c语言:根据输入的分数,判断其所在的等级,使用switch语句
- 第二章习题
- 查找一个数组中出现次数最多的值(长度为1000)
- Java实现算法之Brackets Sequence问题
- java鬼混笔记:springboot之thymeleaf 1:简单的thymeleaf例子
- 设计模式之禅7