统一异常处理
测试demo:Spring+SpringMvc+Mybatis-Plus+SpringBoot+RESTful风格Api +Maven
一,全局异常
1,代码
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
/**
* 查询所有的教师信息
* @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,代码
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
/**
* 查询所有的教师信息
* @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,代码(一)
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());
}
}
代码(二)
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
/**
* 查询所有的教师信息
*
* @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,效果


浙公网安备 33010602011771号