在 Spring Boot 里,异常处理和全局异常处理是保障应用稳定性与用户体验的重要环节。下面详细介绍它们的实现方式。

局部异常处理

在控制器类里,你可以使用 @ExceptionHandler 注解处理特定类型的异常。下面是一个示例:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LocalExceptionController {

    @GetMapping("/local-error")
    public String triggerException() {
        throw new RuntimeException("局部异常触发");
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleRuntimeException(RuntimeException ex) {
        return new ResponseEntity<>("处理局部运行时异常: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

在这个示例中,handleRuntimeException 方法会处理 LocalExceptionController 类里所有的 RuntimeException 异常。

全局异常处理

全局异常处理可以让你在一个地方统一处理应用中的所有异常。要实现全局异常处理,可使用 @ControllerAdvice@RestControllerAdvice 注解定义一个全局异常处理类。

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleAllExceptions(Exception ex) {
        return new ResponseEntity<>("处理全局异常: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
        return new ResponseEntity<>("处理非法参数异常: " + ex.getMessage(), HttpStatus.BAD_REQUEST);
    }
}

在这个示例中,GlobalExceptionHandler 类会处理整个应用中的所有异常。handleAllExceptions 方法会处理所有类型的异常,而 handleIllegalArgumentException 方法专门处理 IllegalArgumentException 异常。

自定义异常类

你还可以自定义异常类,然后在全局异常处理类中处理这些自定义异常。

// 自定义异常类
class CustomException extends RuntimeException {
    public CustomException(String message) {
        super(message);
    }
}

// 控制器类
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomExceptionController {

    @GetMapping("/custom-error")
    public String triggerCustomException() {
        throw new CustomException("自定义异常触发");
    }
}

// 全局异常处理类
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CustomGlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public ResponseEntity<String> handleCustomException(CustomException ex) {
        return new ResponseEntity<>("处理自定义异常: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

在这个示例中,定义了一个 CustomException 类,在 CustomExceptionController 类中触发该异常,然后在 CustomGlobalExceptionHandler 类中处理该异常。

以下是完整的代码示例,包含了上述步骤中的关键代码:

CustomExceptionController.java

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomExceptionController {

    @GetMapping("/custom-error")
    public String triggerCustomException() {
        throw new CustomException("自定义异常触发");
    }
}    

LocalExceptionController.java

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LocalExceptionController {

    @GetMapping("/local-error")
    public String triggerException() {
        throw new RuntimeException("局部异常触发");
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleRuntimeException(RuntimeException ex) {
        return new ResponseEntity<>("处理局部运行时异常: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}    

CustomException.java

class CustomException extends RuntimeException {
    public CustomException(String message) {
        super(message);
    }
}    

CustomGlobalExceptionHandler.java

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CustomGlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public ResponseEntity<String> handleCustomException(CustomException ex) {
        return new ResponseEntity<>("处理自定义异常: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}    

GlobalExceptionHandler.java

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleAllExceptions(Exception ex) {
        return new ResponseEntity<>("处理全局异常: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
        return new ResponseEntity<>("处理非法参数异常: " + ex.getMessage(), HttpStatus.BAD_REQUEST);
    }
}    

通过上述方法,你可以在 Spring Boot 中实现局部异常处理和全局异常处理,确保应用在出现异常时能给用户提供友好的反馈。