SPRING MVC3.2案例讲解--异常处理
来源:互联网 发布:淘宝信誉查询系统 编辑:程序博客网 时间:2024/05/09 18:32
SPRING MVC 异常处理 可以全局进行定义,也可以单独在SPRING controller中定义;同时SPRING MVC将异常处理的代码和业务逻辑进行分离,将异常的处理放到一个单独的带有@ExceptionHandler方法中;
@Controllerpublic class ExceptionController { @RequestMapping("/exception")public @ResponseBody String exception() {throw new IllegalStateException("Sorry!");} // 处理异常的代码,这样就实现了业务逻辑和异常处理的分离@ExceptionHandlerpublic @ResponseBody String handle(IllegalStateException e) {return "IllegalStateException handled!";}@RequestMapping("/global-exception")public @ResponseBody String businessException() throws BusinessException {throw new BusinessException();}}
SPRING 处理异常的流程:
1.WEB启动时ExceptionHandlerExceptionResolver 扫描到通用的异常处理方法:
DEBUG ExceptionHandlerExceptionResolver Looking for exception mappings: WebApplicationContext for namespace 'appServlet-servlet': startup date [Sat Jun 08 08:40:41 GMT 2013]; parent: Root WebApplicationContext
INFO ExceptionHandlerExceptionResolver Detected @ExceptionHandler methods in globalExceptionHandler
2.当请求出现异常时,首先看当前类中是否含有@ExceptionHandler的处理方法
DEBUG ExceptionHandlerExceptionResolver Resolving exception from handler [public java.lang.String org.springframework.samples.mvc.exceptions.ExceptionController.exception()]: java.lang.IllegalStateException: Sorry!
DEBUG ExceptionHandlerExceptionResolver Invoking @ExceptionHandler method: public java.lang.String org.springframework.samples.mvc.exceptions.ExceptionController.handle(java.lang.IllegalStateException)
DEBUG RequestResponseBodyMethodProcessor Written [IllegalStateException handled!] as "text/plain;charset=ISO-8859-1" using [org.springframework.http.converter.StringHttpMessageConverter@163e9a1]
3.然后看有没有含有@ExceptionHandler method 的类,另外该类必须@ControllerAdvice
DEBUG ExceptionHandlerExceptionResolver Resolving exception from handler [public java.lang.String org.springframework.samples.mvc.exceptions.ExceptionController.businessException() throws org.springframework.samples.mvc.exceptions.BusinessException]: org.springframework.samples.mvc.exceptions.BusinessException
DEBUG ExceptionHandlerExceptionResolver Invoking @ExceptionHandler method: public java.lang.String org.springframework.samples.mvc.exceptions.GlobalExceptionHandler.handleBusinessException(org.springframework.samples.mvc.exceptions.BusinessException)
- SPRING MVC3.2案例讲解--异常处理
- SPRING MVC3.2案例讲解--SPRING MVC3的POJO赋值
- SPRING MVC3.2案例讲解--SPRING MVC3的服务端验证
- SPRING MVC3.2案例讲解---配置
- SPRING MVC3.2案例讲解--fileupload
- SPRING MVC3.2案例讲解--SPRING定义欢迎首页
- SPRING MVC3.2案例讲解---如何获取请求参数
- SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射(2)
- SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射(1)
- SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射+JSP视图跳转(3)
- SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity
- SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射(3)
- SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity
- spring mvc3中异常处理
- spring mvc3中异常处理
- SPRING MVC3.2案例讲解--使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- SPRING MVC3.2案例讲解--使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- SPRING MVC3.2案例讲解-请求重定向与请求转发
- 遗传算法小试刀
- SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射(3)
- SPRING MVC3.2案例讲解--使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务
- WEBLOGIC单机集群log4j的设置
- SPRING MVC3.2案例讲解--fileupload
- SPRING MVC3.2案例讲解--异常处理
- SPRING MVC3.2案例讲解-请求重定向与请求转发
- 利用SPRINGMVC3.2改造旧系统
- 如何把物理机上的文件复制到virtualbox虚拟机
- DWR与SPRING 集成
- SPRING MVC上传的中文乱码问题
- GIS编辑中出现ORA-00001:违反唯一约束条件
- [置顶] 利用spring mvc,hibernate重构系统(1)
- unity3d学习笔记(五)--结合Mecanim实现怪物AI