一、@ControllerAdvice + @ExceptionHandler
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 处理所有不可知的异常
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseBody
AppResponse handleException(Exception e){
LOGGER.error(e.getMessage(), e);
AppResponse response = new AppResponse();
response.

本文介绍了SpringMVC中处理全局异常的两种方法:一是使用@ControllerAdvice配合@ExceptionHandler注解,创建GlobalExceptionHandler类捕获并处理异常;二是实现HandlerExceptionResolver接口,自定义异常处理逻辑,确保该类在Spring容器中。
1万+

被折叠的 条评论
为什么被折叠?



