SpringBoot使用自定义异常
SpringBoot使用自定义异常 1.自定义异常 public class CustomException extends RuntimeException { public CustomException(String message) { super(message); } } 2.处理自定义异常 @ControllerAdvice public class GlobalExceptionHandler { // 处理自定义异常 @ExceptionHandler(CustomException.class) public ResponseEntity<Object> handleCustomException(CustomException ex) { Map<String, Object> body = new HashMap<>(); body.put("code", 200); body.put("msg", ex.getMessage()); body.put("data", data); return new ResponseEntity<>(body, HttpStatus.OK); }
