首页 > 技术文章 > 异常统一处理

jerrybu 2021-03-06 13:54 原文


/**
* 自定义异常类
*/
@Data
@AllArgsConstructor //有参数构造器
@NoArgsConstructor //生成无参数构造
public class GuliException extends RuntimeException {
private Integer code;//状态码
private String msg;//输出消息
}



@Slf4j("日志统一管理")
@ControllerAdvice
public class Globalexceptionhandler {

//指定异常执行方法
@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public R error(ArithmeticException e){
e.printStackTrace();
return R.error().message("方法执行ArithmeticException异常!");
}

//自定义的异常处理
@ExceptionHandler(com.atguigu.servicebaes.exceptionhandler.GuliException.class)
@ResponseBody
public R error(com.atguigu.servicebaes.exceptionhandler.GuliException e){
log.error(e.getMessage());
e.printStackTrace();
return R.error().code(e.getCode()).message(e.getMsg());
}

}





推荐阅读