好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot框架的全局异常处理方案详解

系统框架搭建的前期过程中,为了约束代码规范,我们会对一些通用功能做一些处理,比如声明一些系统公用错误类、封装通用返回结果、统一异常处理等,这样做的优势是团队开发过程中能够形成统一的代码规范,增强代码可读性,同时又便于后期代码维护。本文主要介绍下框架中异常的处理:

1.声明全局异常处理类,并添加@controlleradvice和@restcontroller注解
代码如下:

?

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

@controlleradvice

@restcontroller

public class globalexceptionhandler {

    

     private final logger logger = logger.getlogger(globalexceptionhandler. class );

 

     //空指针异常

   @exceptionhandler (nullpointerexception. class )

   public map<string, object> nullpointer(nullpointerexception e,httpservletrequest req){

     logger.error(e.getmessage());

         e.printstacktrace();

     return resultobject.newresultobj(errorcode.nullpointer);

   }

    

     //io异常

     @exceptionhandler (ioexception. class ,httpservletrequest req, httpservletresponse res)

   public map<string, object> ioexception(ioexception e){

      logger.error(e.getmessage());

          e.printstacktrace();

      return resultobject.newresultobj(errorcode.ioexception);

   }

  

    

     //权限不足异常   

     @exceptionhandler (unauthorizedexception. class )

     public map<string, object> unauth(httpservletrequest req, httpservletresponse res){

         return resultobject.newresultobj(errorcode.unauth);

     }

    

     //未登录异常

     @exceptionhandler (authorizationexception. class )

     public map<string, object> unlogin(httpservletrequest req, httpservletresponse res){

         return resultobject.newresultobj(errorcode.notlogin);

     }

    

     //其它异常

     @exceptionhandler (exception. class )

     public map<string, object> error(exception e,httpservletrequest req, httpservletresponse res){

         logger.error(e.getmessage());

         e.printstacktrace();

         return resultobject.newresultobj(errorcode.system);

     }

    

}

2.声明系统通用错误处理类

?

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

45

46

47

48

49

50

51

52

53

/**

  * 系统通用错误处理类

  */

public class errorcode {

 

   //返回码

   private int code = 0 ;

    

   //描述信息

   private string errmsg = "success" ;

    

   //成功

   public static final errorcode success                       = new errorcode( 0 , "success" );

   public static final errorcode params                    = new errorcode( 100000 , "参数错误" );

   public static final errorcode system                    = new errorcode( 100001 , "系统错误" );

     public static final errorcode unlogin                     = new errorcode( 100002 , "未登录" );

   public static final errorcode unauth                    = new errorcode( 100003 , "权限错误" );

     public static final errorcode nullpointer                   = new errorcode( 100004 , "空指针错误" );

     public static final errorcode ioexception                   = new errorcode( 100005 , "io错误" );

   public static final errorcode upload_fail                     = new errorcode( 100006 , "文件上传失败" );

   public static final errorcode repeat                    = new errorcode( 100007 , "数据已存在" );

 

   public errorcode() {}

 

   @override

   public string tostring() {

     return string.format( "{\"errmsg\": \"%s\", \"code\": %d}" , errmsg, code);

   }

 

   public int getcode() {

     return code;

   }

 

   public errorcode setcode( int code) {

     this .code = code;

     return this ;

   }

 

   public string geterrmsg() {

     return errmsg;

   }

 

   public errorcode seterrmsg(string errmsg) {

     this .errmsg = errmsg;

     return this ;

   }

 

   public errorcode( int code, string errmsg) {

     this .code = code;

     this .errmsg = errmsg;

   }

 

}

3.声明全局异常处理类中的结果返回类

?

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

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

import java.util.hashmap;

import java.util.map;

import javax.servlet.http.httpservletresponse;

import com.xxx.errorcode; //此处引用自己errorcode类所在的包路径

import org.json.jsonobject;

 

public class resultobject {

    

     /**

      * 输出错误信息

      * @param errorcode

      */

     public static map<string, object> newresultobj(errorcode errorcode){

         map<string, object> obj = new hashmap<string, object>();

         obj.put( "code" , errorcode.getcode());

         obj.put( "errmsg" , errorcode.geterrmsg());

         return obj;

     }

    

     /**

      * 输出带数据的成功信息

      * @param data

      */

     public static map<string, object> newresultobj(object data){

         map<string, object> obj = new hashmap<string, object>();

         obj.put( "code" , errorcode.success.getcode());

         obj.put( "errmsg" , errorcode.success.geterrmsg());

         if ( null != data){

             obj.put( "data" , data);

         }

         return obj;    

     }

    

     /**

      * 输出带数据的错误信息

      * @param errorcode

      * @param data

      */

     public static map<string, object> newresultobj(errorcode errorcode, object data){

         map<string, object> obj = new hashmap<string, object>();

         obj.put( "code" , errorcode.getcode());

         obj.put( "errmsg" , errorcode.geterrmsg());

         if ( null != data){

             obj.put( "data" , data);

         }

         return obj;            

     }

    

    

     /**

      * 输出结果

      */

     public static void outputresult(httpservletresponse response, errorcode errorcode){

         try {

             jsonobject json = new jsonobject();

             json.put( "code" , errorcode.getcode());

             json.put( "errmsg" , errorcode.geterrmsg());

            

             response.setcharacterencoding( "utf-8" );

             response.setcontenttype( "application/json;charset=utf-8" );

             response.getwriter().write(json.tostring());

         } catch (exception e1) {

         }

     }

}

到此这篇关于springboot框架的全局异常处理方案的文章就介绍到这了,更多相关springboot全局异常处理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_34279303/article/details/114680758

查看更多关于springboot框架的全局异常处理方案详解的详细内容...

  阅读:11次