统一异常处理

测试demo:Spring+SpringMvc+Mybatis-Plus+SpringBoot+RESTful风格Api +Maven

一,全局异常

1,代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mangoubiubiu.exception;
 
 
import com.mangoubiubiu.commonutils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
@ControllerAdvice
public class GlobalExceptionHandler {
    //全局异常处理
    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody//返回数据
    public R error(Exception e){
     return R.error().message("执行了全局异常处理");
    }
 
}

2,异常的cotroller

1
2
3
4
5
6
7
8
9
10
11
12
/**
 * 查询所有的教师信息
 * @return
 */
@ApiOperation(value = "所有的教师信息")
@GetMapping("findAll")
public R findAllTeacher(){
 
        int test=10/0;
    List<Teacher>  list= service.list(null);
    return R.ok().data("items",list);
}

3,效果

 

 

 

二,特殊的异常处理:注意点,全局异常和特殊异常一起定义时 会先找比全局异常范围小的特殊异常

1,代码

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
package com.mangoubiubiu.exception;
 
 
import com.mangoubiubiu.commonutils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
@ControllerAdvice
public class GlobalExceptionHandler {
    //全局异常处理
    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody//返回数据
    public R error(Exception e){
     return R.error().message("执行了全局异常处理");
    }
    //找异常的话从从特殊到全局
    //特殊异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody//返回数据
    public R errorArithmeticException(ArithmeticException e){
        return R.error().message("特殊异常处理");
    }
 
}

  

2,异常的cotroller

1
2
3
4
5
6
7
8
9
10
11
12
/**
   * 查询所有的教师信息
   * @return
   */
  @ApiOperation(value = "所有的教师信息")
  @GetMapping("findAll")
  public R findAllTeacher(){
 
          int test=10/0;
      List<Teacher>  list= service.list(null);
      return R.ok().data("items",list);
  }

3,效果

 

 

三,自定义异常

 1,代码(一)

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
package com.mangoubiubiu.exception;
 
 
import com.mangoubiubiu.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
@ControllerAdvice
@Slf4j //把异常信息写到文件中,1,加一个slf4j 2,    在异常方法里面加上  log.error(e.getMessage());
public class GlobalExceptionHandler {
    //全局异常处理
    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody//返回数据
    public R error(Exception e){
     return R.error().message("执行了全局异常处理");
    }
    //找异常的话从从特殊到全局
    //特殊异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody//返回数据
    public R errorArithmeticException(ArithmeticException e){
        return R.error().message("特殊异常处理");
    }
 
    //找异常的话从从特殊到全局
    //自定义处理
    @ExceptionHandler(MyException.class)
    @ResponseBody//返回数据
     public R errorGuiLiException(MyException e){
        return R.error().code(e.getCode()).message(e.getMsg());
    }
}

  代码(二)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.mangoubiubiu.exception;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
//生成getset方法
@Data
//有参构造
@AllArgsConstructor
//无参构造
@NoArgsConstructor
public class MyException extends RuntimeException{
 
     private Integer code;//状态码
 
     private String msg;//异常信息
 
}

2,异常的cotroller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 查询所有的教师信息
 *
 * @return
 */
@ApiOperation(value = "所有的教师信息")
@GetMapping("findAll")
public R findAllTeacher() {
 
    try {
        int test = 10 / 0;
 
    } catch (Exception e) {
 
        throw new MyException(10440, "执行了自定义异常处理");
    }
 
    List<Teacher> list = service.list(null);
    return R.ok().data("items", list);
}

  3,效果

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/13734566.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(202)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起