案例一:
自定义异常
public class StudentException extends RuntimeException {private Integer code;private String msg;public StudentException(Integer code, String msg) {this.code = code;this.msg = msg;}/*** 这里需要重写 getMessage方法,否则会报控指针错误* getMessage:输出异常类的报错信息** @return*/@Overridepublic String getMessage() {return this.getMsg();}public String getMsg() {return msg;}public Integer getCode() {return code;}
}
异常信息类枚举类
指定错误规则,方便区分错误信息。
public enum ExecptionStatusEnum {STUDENT_ERROR_EXECPTION(01, "学号错误"), STUDENT_UNORDER_EXECPTION(02, "学号不存在");private Integer code;private String msg;ExecptionStatusEnum(Integer code, String msg) {this.code = code;this.msg = msg;}public Integer getCode() {return code;}public String getMsg() {return msg;}
}
全局异常处理
自定义返回类
public class Result extends HashMap<String, Object> {/*** 状态码*/public static final String CODE_TAG = "code";/*** 返回内容*/public static final String MSG_TAG = "msg";/*** 数据对象*/public static final String DATA_TAG = "data";public Result() {}public Result(Integer code, String msg, Object data) {super.put(CODE_TAG, code);super.put(MSG_TAG, msg);if (data != null) {super.put(DATA_TAG, data);}}public static Result ok() {return Result.ok(200, "操作成功");}public static Result ok(Integer code, String msg) {return new Result(code, msg, null);}public static Result error() {return Result.ok(500, "系统错误");}public static Result error(Integer code, String msg) {return new Result(code, msg, null);}public static Result ok(Object data) {return new Result(200, "操作成功", data);}
}
全局异常处理类
@RestControllerAdvice
public class GlobalExceptionHandler {/*** 自定义异常类*/@ExceptionHandler(StudentException.class)public Result handleStudentException(StudentException e) {return Result.error(e.getCode(), e.getMsg());}/*** 其他异常*/@ExceptionHandler(Exception.class)public Result handleException(Exception e) {return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());}}
Controller层
@RestController
public class StudentController {@GetMapping("/student/{id}")public Result test(@PathVariable("id") Long id) {if (id == 1) {// 抛出异常:学生错误throw new StudentException(StudentExecptionStatusEnum.STUDENT_ERROR_EXECPTION.getCode(),StudentExecptionStatusEnum.STUDENT_ERROR_EXECPTION.getMsg());} else if (id == 2) {// 抛出异常:学生不存在throw new StudentException(StudentExecptionStatusEnum.STUDENT_UNORDER_EXECPTION.getCode(),StudentExecptionStatusEnum.STUDENT_ERROR_EXECPTION.getMsg());}// 返回 学生信息return Result.ok(new Student(4, "zqsl1234"));}
}
案例二:
自定义异常类
public class MyException extends RuntimeException{public MyException(){super();}public MyException(String message){super(message);}public MyException(Throwable cause){super(cause);}public MyException(String message,Throwable cause){super(message,cause);}
}
全局异常处理类
@RestControllerAdvice
public class GlobalExceptionHandler {/*** 自定义异常类*/@ExceptionHandler(MyException.class)public ResponseEntity<String> handleMyException(MyException e) {return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}/*** 其他异常*/@ExceptionHandler(Exception.class)public Result handleException(Exception e) {return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);}}
测试
public class Test {public void test(){throw new MyException("我是自定义报错信息");}public static void main(String[] args){Test test = new Test();test.test();}
}