service-base 中创建统一异常处理类 GlobalExceptionHandler
异常处理类:根据不同的异常 return R对象
@ControllerAdvice
public class GlobalExceptionHandler {
//指定出现什么异常执行这个方法
@ExceptionHandler(Exception.class)
@ResponseBody //为了返回数据
public R error(Exception e){
e.printStackTrace();
return R.error().message("执行了全局的异常类...");
}
}
//特定异常
@ExceptionHandler(ArithmeticException.class)
@ResponseBody//为了返回数据
public R error(ArithmeticException e){
e.printStackTrace();
return R.error().message("执行了ArithmeticException的异常类...");
}
//自定义异常
@ExceptionHandler(GuliException.class)
@ResponseBody//为了返回数据
public R error(GuliException e){
e.printStackTrace();
return R.error().message(e.getMsg()).code(e.getCode());
}
自定义异常 : 状态码、信息
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GuliException extends RuntimeException{
@ApiModelProperty(value = "状态码")
private Integer code;
private String msg;
}