在 Spring Boot 中,异常处理是一个重要的方面,而且有多种方式可以处理异常。以下是一些常见的 Spring Boot 异常处理方式:
-
使用
@ControllerAdvice注解:@ControllerAdvice注解用于定义全局异常处理器。通过在一个类上标记@ControllerAdvice注解,可以集中处理应用程序中抛出的异常。
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> handleException(Exception e) { return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } -
使用
@ExceptionHandler注解:@ExceptionHandler注解用于在特定的 Controller 类中处理异常。通过在 Controller 类的方法上标记该注解,可以处理特定异常类型的异常。
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @ExceptionHandler(RuntimeException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ResponseEntity<String> handleRuntimeException(RuntimeException e) { return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } -
使用
ResponseEntityExceptionHandler:- 继承
ResponseEntityExceptionHandler类,可以自定义处理异常的方法,并使用@ExceptionHandler注解标记这些方法。
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @ControllerAdvice @RestController public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ResponseEntity<String> handleException(Exception e) { return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } - 继承
-
使用
@RestControllerAdvice:@RestControllerAdvice是@ControllerAdvice和@ResponseBody的组合,用于定义全局异常处理器,并直接返回 JSON 格式的响应。
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice public class GlobalRestControllerAdvice { @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ResponseEntity<String> handleException(Exception e) { return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } }
这些方式可以根据具体需求选择,通常全局异常处理器适用于处理整个应用程序中的异常,而局部的 @ExceptionHandler 则适用于处理特定 Controller 或方法中的异常。
Was this helpful?
0 / 0