Springboot全局异常捕捉处理和自定义全局异常处理

2019-10-07 21:02:16

参考地址  Springboot2.0全局异常捕捉处理和自定义全局异常处理

一,全局异常捕捉处理

新建MyControllerAdvice类,建在包下都有作用:


import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

import java.util.Map;


/**

 * @Auther: cookie

 * @Date: 2018/7/26 15:09

 * @Description: 全局捕获异常和自定义全局捕获异常

 */

@ControllerAdvice  //不指定包默认加了@Controller和@RestController都能控制

//@ControllerAdvice(basePackages ="com.example.demo.controller")

public class MyControllerAdvice {


    /**

     * 全局异常处理,反正异常返回统一格式的map

     * @param ex

     * @return

     */

    @ResponseBody

    @ExceptionHandler(value = Exception.class)

    public Map<String,Object> exceptionHandler(Exception ex){

        Map<String,Object> map  = new HashMap<String,Object>();

        map.put("code",1001);

        map.put("mag",ex.getMessage());

        //发生异常进行日志记录,写入数据库或者其他处理,此处省略

        return map;

     }

    }

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

测试:

controller下的方法:


 @RequestMapping("/{id}")

      public String test(@PathVariable Integer id){

        if(true){

          id=1/id;

        }

      return "success";

      }

1

2

3

4

5

6

7

结果:



id为0时,1除以0异常。


拦截捕捉自定义异常 MyException.class

我的异常类:


/**

 * @Auther: cookie

 * @Date: 2018/7/26 15:22

 * @Description:

 */

public class MyException extends RuntimeException{


    private String code;  //异常状态码


    private String message;  //异常信息


    private String method;   //发生的方法,位置等


    private String descinfo;   //描述


    public MyException(String code, String message, String method, String descinfo) {

        this.code=code;

        this.message=message;

        this.method=method;

        this.descinfo=descinfo;

    }


    public String getCode() {

        return code;

    }


    public void setCode(String code) {

        this.code = code;

    }


    public String getMessage() {

        return message;

    }


    public void setMessage(String message) {

        this.message = message;

    }


    public String getMethod() {

        return method;

    }


    public void setMethod(String method) {

        this.method = method;

    }


    public String getDescinfo() {

        return descinfo;

    }


    public void setDescinfo(String descinfo) {

        this.descinfo = descinfo;

    }

}

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

import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

import java.util.Map;


/**

 * @Auther: cookie

 * @Date: 2018/7/26 15:09

 * @Description: 全局捕获异常和自定义全局捕获异常

 */

@ControllerAdvice  //不指定包默认加了@Controller和@RestController都能控制

//@ControllerAdvice(basePackages ="com.example.demo.controller")

public class MyControllerAdvice {


    /**

     * 拦截捕捉自定义异常 MyException.class

     * @param myex

     * @return

     */

    @ResponseBody

    @ExceptionHandler(value = MyException.class)

    public Map<String,Object> myExceptionHandler(MyException myex){

        Map<String,Object> map  = new HashMap<String,Object>();

        map.put("code",myex.getCode());

        map.put("message",myex.getMessage());

        map.put("method",myex.getMethod());

        map.put("descinfo",myex.getDescinfo());

        //发生异常进行日志记录,写入数据库或者其他处理,此处省略

        return map;

    }




}

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

测试:


@RequestMapping(value = "/")

      public String index() throws Exception{

       String name =  redisUtil.set("key100", "666");

       if(StringUtils.isEmpty(name)){

         throw new MyException("1001","empty","/API/getUserName","在获取用户名字的时候为空");

       }

           return name;

      }

1

2

3

4

5

6

7

8

结果:




  • 2019-05-21 12:35:54

    RecyclerView setHasFixedSize(true)的意义

    设置为true,再调用notifyDataSetChanged(),发现大小重新计算了,看来理解出现错误了。还是再看一下哪些地方用到这个mHasFixedSize吧。

  • 2019-05-21 12:37:34

    RecyclerView 刷新闪烁

    闪烁是 notifyDataSetChange 造成的。由于适配器不知道整个数据集中的哪些内容已经存在,在重新匹配 ViewHolder 时发生的。 当然,遇到这个问题时有去搜索一些答案,看到的很多都是去禁止 RecycleView 的默认动画,可惜这对我没什么用。下面的方法是对我有用的。

  • 2019-05-21 12:43:26

    (重要)RecycleView的缓存机制

    RecycleView的四级缓存是由三个类共同作用完成的,Recycler、RecycledViewPool和ViewCacheExtension。Recycler用于管理已经废弃或者与RecyclerView分离的ViewHolder,这里面有两个重要的成员,为可以看见的屏幕的内部缓存成员mAttachedScrap、mChangedScrap和滑出屏幕外的外部缓存成员mCachedViews二者共同完成ViewHolder的缓存;RecycledViewPool类是用来缓存整体所有的ViewHolder,是对mCachedViews缓存的补充;ViewCacheExtension是扩展内的缓存对象,默认不加载,需实现方法getViewForPositionAndType(Recycler recycler, int position, int type)来实现自己的缓存。接下来对四级缓存一步步介绍。

  • 2019-05-21 12:44:31

    对嵌套RecyclerView的优化

    RecyclerView 是一个更高级的 ListView ,它可以重用view避免额外创建太多的view从而带来流畅的滚动性能。RecyclerView通过叫做 View pool 的东西持有不再可见的 view ,让它可被回收

  • 2019-05-25 14:54:50

    commit your changes or stash them before you can merge

    Your local changes to the following files would be overwritten by merge:         protected/config/main.php Please, commit your changes or stash them before you can merge. --------------------- 作者:陈小峰_iefreer 来源:CSDN 原文:https://blog.csdn.net/iefreer/article/details/7679631 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2019-05-27 10:43:34

    IntelliJ IDEA中C盘文件过大怎么办

    当我在D:\ 安装完IDEA9.0之后,建立了一个工程,发现C:\Users\Administrator\.IntelliJIdea90 竟然增大到了500+M,并且随着使用在逐渐增大,这样占用系统盘资源是非常让人不爽的,那么如何将其修改到其他路径呢?