Spring MVC 系统异常处理方式及性能对比

本文详细介绍了SpringMVC系统异常处理的三种方式:使用SimpleMappingExceptionResolver、实现HandlerExceptionResolver接口自定义异常处理器以及使用@ExceptionHandler注解实现异常处理。同时,概述了未捕获异常的处理方法及不同异常处理方式的优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring MVC 系统异常处理方式及性能对比

源自:http://my.oschina.net/CandyDesire/blog/333340

 

Spring MVC 系统异常处理方式及性能对比

        发表于6个月前(2014-10-17 17:19)      阅读( 1917) | 评论( 2)               28人收藏此文章, 我要收藏    
赞2

慕课网,程序员升职加薪神器,点击免费学习

            

摘要             Spring MVC处理异常的3种方式
SpringMVC                Exception                异常处理                SimpleMappingExceptionResolver                HandlerExceptionResolver                @ExceptionHandler                   


 

大部分公司所用的Spring框架版本是3.1版本以下,所以今天暂时总结3.1版本的Spring-MVC异常处理方式。

一、Spring MVC处理异常有3种方式: 
(1)使用Spring-MVC提供的SimpleMappingExceptionResolver; 
(2)实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器; 
(3)使用@ExceptionHandler注解实现异常处理; 

二、分别介绍这三种异常处理的实现方式:

(1)使用SimpleMappingExceptionResolver实现异常处理

          只需要在Spring的配置文件applicationContext.xml中增加以下内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< bean  class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >  
     <!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->  
     < property  name = "defaultErrorView"  value = "error" ></ property >  
     <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->  
     < property  name = "exceptionAttribute"  value = "ex" ></ property >  
     <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常以页名作为值 -->  
     < property  name = "exceptionMappings" >  
         < props >  
             <!-- 创建自己所要自定义的异常类 -->
             < prop  key = "com.core.exception.BusinessException" >business_error</ prop >  
             < prop  key = "com.core.exception.ParameterException" >parameter_error</ prop >  
             <!-- 还可以继续扩展对不同异常类型的处理 -->  
         </ props >  
     </ property >  
</ bean >


(2) 实现HandlerExceptionResolver 接口自定义异常处理器

  首先增加HandlerExceptionResolver 接口的实现类MyExceptionHandler,代码如下: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public  class  MyExceptionHandler  implements  HandlerExceptionResolver {  
   
     public  ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, 
         Object handler, Exception ex) {  
         Map<String, Object> model =  new  HashMap<String, Object>();  
         model.put( "ex" , ex);  
         // 根据不同错误转向不同页面  
         if (ex  instanceof  BusinessException) {  
             return  new  ModelAndView( "business_error" , model);  
         } else  if (ex  instanceof  ParameterException) {  
             return  new  ModelAndView( "parameter_error" , model);  
         else  {  
             return  new  ModelAndView( "error" , model);  
         }  
     }  
}


    然后在Spring的配置文件applicationContext.xml中增加以下内容:

?
1
< bean  id = "exceptionHandler"  class = "com.core.exception.MyExceptionHandler" />


   (3)使用@ExceptionHandler注解实现异常处理 

     首先要增加BaseController类,并在类中使用@ExceptionHandler注解声明异常处理,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  class  BaseController {  
     /** 基于@ExceptionHandler异常处理 */  
     @ExceptionHandler  
     public  String exp(HttpServletRequest request, Exception ex) {  
         request.setAttribute( "ex" , ex);  
         // 根据不同错误转向不同页面  
         if (ex  instanceof  BusinessException) {  
             return  "business_error" ;  
         } else  if (ex  instanceof  ParameterException) {  
             return  "parameter_error" ;  
         else  {  
             return  "error" ;  
         }  
     }  
}


    然后需要修改现有代码,使所有需要异常处理的Controller都继承该类,如下所示:

?
1
public  class  TestController  extends  BaseController

三、未捕获异常的处理 :
        对于Unchecked Exception而言,由于代码不强制捕获,往往被忽略,如果运行期产生了UncheckedException,而代码中又没有进行相应的捕获和处理,则我们可能不得不面对尴尬的404、500……等服务器内部错误提示页面。 
        我们需要一个全面而有效的异常处理机制。目前大多数服务器也都支持在Web.xml中通过<error-page>(Websphere/Weblogic)或者<error-code>(Tomcat)节点配置特定异常情况的显示页面。

实现方式如下:

修改web.xml文件,增加以下内容: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 出错页面定义 -->  
< error-page >  
     < exception-type >java.lang.Throwable</ exception-type >  
     < location >/500.jsp</ location >  
</ error-page >  
< error-page >  
     < error-code >500</ error-code >  
     < location >/500.jsp</ location >  
</ error-page >  
< error-page >  
     < error-code >404</ error-code >  
     < location >/404.jsp</ location >  
</ error-page >  
<!-- 可继续增加服务器错误号的处理及对应显示页面 -->


四、比较异常处理方式的优缺点:

        Spring MVC集成异常处理3种方式都可以达到统一异常处理的目标。从3种方式的优缺点比较,若只需要简单的集成异常处理,推荐使用SimpleMappingExceptionResolver即可;若需要集成的异常处理能够更具个性化,提供给用户更详细的异常信息,推荐自定义实现HandlerExceptionResolver接口的方式;若不喜欢Spring配置文件或要实现“零配置”,且能接受对原有代码的适当入侵,则建议使用@ExceptionHandler注解方式。 


在此补充个Ajax实现异常信息无跳转的处理方式:

   Java代码(Controller类)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   @RequestMapping (value =  "/ajaxAlert" , method = RequestMethod.POST)  
     public  void  ajax(HttpServletResponse response, User user)  throws  Exception {  
 
         if (user.getId()== null ) {  
             if (user.getUserName()== null  ||  "" .equals(user.getUserName())) {  
         AjaxUtils.rendJson(response,  false "用户名为空创建失败" ); 
             else  {  
                 AjaxUtils.rendJson(response,  true "创建用户成功" );  
             }  
         else  {  
             AjaxUtils.rendJson(response,  true "修改用户成功" );  
         }
     }
     
      @RequestMapping (value =  "/controllerAjax" , method = RequestMethod.POST)
     public  void  controllerAjax(HttpServletResponse response, Integer id)  throws  Exception {    
         try 
             testService.daoTest(); 
             AjaxUtils.rendJson(response,  true "操作成功" );    
         catch (Exception be) {    
         AjaxUtils.rendJson(response,  false "Dao层异常" );       
         }    
     }


  Java代码(AjaxUtils类)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import  java.io.IOException;
import  javax.servlet.http.HttpServletResponse;
import  net.sf.json.JSONObject;
 
public  class  AjaxUtils {
 
     public  static  void  rendText(HttpServletResponse response, String content)  
         throws  IOException {  
         response.setCharacterEncoding( "UTF-8" );  
         response.getWriter().write(content);  
     }    
       
     public  static  void  rendJson(HttpServletResponse response,  boolean  success,
          String message)  throws  IOException{  
         JSONObject json =  new  JSONObject();  
         json.put( "isSuccess" , success);  
         json.put( "message" , message);  
         rendText(response, json.toString());  
     }    
}

    html代码(jQuery)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<! DOCTYPE  html>  
< html >  
  < head >  
   < meta  http-equiv = "Content-Type"  content = "text/html; charset=UTF-8" >   
   < title >Exception</ title >  
   < script  type = "text/javascript"  src = "./jquery-1.10.2.min.js" ></ script >  
   < script  type = "text/javascript" >  
   $(function(){  
         init();  
     });  
     
   function init(){  
         $("#ajaxCreate").click(function(){  
             doAjax({userName: "Candy", realName: "Carol"});  
         })  
         $("#ajaxUpdate").click(function(){  
             doAjax({id: 1, userName: "Candy", realName: "Carol"});  
         })  
         $("#ajaxFail").click(function(){  
             doAjax({realName: "Carol"});  
         })  
     }  
   function doAjax(data) {  
       $.post("./ajax.do",data,function(t){  
             if(!t.isSuccess){  
                 alert("操作失败, 原因:" + t.message);  
             }else{  
                 alert("操作成功, 描述:" + t.message);  
             }  
         },"json").error(function(){  
             alert("未知错误");  
         });  
   }  
   </ script >  
  </ head >  
< body >  
   < br  />  
   < a  id = "ajaxCreate"  href = "#" >创建用户成功</ a >  
   < br  />  
   < a  id = "ajaxUpdate"  href = "#" >修改用户成功</ a >     
   < br  />  
   < a  id = "ajaxFail"  href = "#" >用户名为空创建失败</ a >  
</ body >  
</ html >



分享到:     2
  声明:OSCHINA 博客文章版权属于作者,受法律保护。未经作者同意不得转载。     
       

 

评论2

  •    
    1楼: gembin <span class="ng-scope" "="" ng-if="(commentjson.client_type==4)">iPhone 发表于 2014-10-20 08:56           回复此评论   
    性能对比在哪?
  •    
    2楼: Candy_Desire 发表于 2014-10-20 10:00           回复此评论   
    引用来自“gembin”的评论
    性能对比在哪?
    第四点是性能的比较,这里就是概述了一下,没有做具体的数据测试。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值