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

结果:




  • 2020-04-27 11:02:21

    CSS 气泡对话框

    点击上面地址,我们可以看到一个灵活的css气泡,拖动按钮,改变气泡效果。

  • 2020-04-27 16:59:23

    Docker部署nuxtjs

    将一下文件拷贝至服务器目录/app |名称|描述| |-|-| |.nuxt|编译后生成的目录,开发模式和发布模式通用,注意发布前使用npm run build 防止将dev目录发布| |static| 静态资源文件,通过/可直接访问| |package.json|npm 包管理配置文件| |nuxt.config.js|Nuxt.js 默认的配置涵盖了大部分使用情形,可通过 nuxt.config.js 来覆盖默认的配置。| |node_modules|依赖模块|

  • 2020-04-28 10:47:16

    currentColor css 的使用

    以前我们在如何动态设置伪元素的颜色上非过不少心思,看完下面的文章,你就会超级见到那的设置伪元素的颜色了。

  • 2020-04-28 10:48:39

    使用currentColor设置SVG样式

    css自己也又很多变量哦,大家还要多多学习哦。CSS中的变量很有用,因为它们允许我们写更多DRY(不再自我重复,也就是不需要一直写重复内容的代码)代码。它们对于管理和维护大型项目也有非常好用,因为它们包含了大量重复的值。

  • 2020-04-29 20:57:07

    mac卸载avast

    第一种就是卸载。打开主界面,在左上角Avast一栏中选择卸载。卸载完之后要么再装回去,要么去找找别的杀软吧