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);
    }
分类: 标签: SpringBoot 自定义异常

评论

全部评论