标题 自定义异常类
1.先写一个类 继承 RuntimeException
如:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MoneyException extends RuntimeException{
private Integer code; //状态码
private String msg; //异常信息
}
2.然后在写一个类
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody //响应json格式
@ExceptionHandler(MoneyException.class)
public R error(MoneyException e){
e.printStackTrace();
return R.error().code(e.getCode()).message(e.getMsg());
}
}
R是自义定的返回数据类型。