服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot 全局异常处理——解决404/500

SpringBoot 全局异常处理——解决404/500

2023-10-05 12:07CN-ZHANG Java教程

SpringBoot要全局处理 Spring Boot 应用程序中的 HTTP 404 和 500 错误,您可以自定义一个异常处理器类,并使用。下面是一种可能的实现方式,它捕获。注释来拦截和处理这些异常。

SpringBoot 全局异常处理——解决404/500

SpringBoot要全局处理 Spring Boot 应用程序中的 HTTP 404 和 500 错误,您可以自定义一个异常处理器类,并使用 @ControllerAdvice 和 @ExceptionHandler 注释来拦截和处理这些异常。

解决方案

下面是一种可能的实现方式,它捕获HTTP GET请求中PathVariable参数类型不匹配、参数格式错误以及其他所有未处理的异常,并返回一个包含错误代码和错误消息的 Map 对象:

@ControllerAdvice
public class GlobalExceptionHandler {
     
    // 捕获 PathVariable 参数类型不匹配或格式错误的异常,并返回错误信息
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Map handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
        return createErrorResponse(HttpStatus.BAD_REQUEST.value(), "请求参数有误: " + ex.getMessage());
    }
 
    // 捕获其他未处理的异常,并返回错误信息
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Map handleUncaughtException(Exception ex) {
        return createErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部错误: " + ex.getMessage());
    }
 
    // 创建包含错误码和错误消息的 Map 对象
    private Map createErrorResponse(int code, String message) {
        Map errorResponse = new HashMap<>();
        errorResponse.put("code", code);
        errorResponse.put("message", message);
        return errorResponse;
    }
}

说明:

在此示例中:

  • 使用 @ControllerAdvice 和 @ExceptionHandler 注释标识此类为全局异常处理程序,并捕获了 MethodArgumentTypeMismatchException 和任何其他未处理的异常。
  • 使用 @ResponseStatus(HttpStatus.BAD_REQUEST) 和 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 来指定异常的 HTTP 响应状态码。
  • 使用@ResponseBody注释来告诉 Spring Boot 应用程序,我们不想呈现采用模板引擎进行渲染的视图,而是返回具体的响应正文对象。

    createErrorResponse() 方法创建并返回包含错误代码和错误消息的 Map 对象。

通用的全局异常处理

@ControllerAdvice
public class GlobalExceptionHandler {
    
    /**
     * 处理 HTTP 404 错误
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public ApiError handleNotFound(HttpServletRequest req, Exception ex) {
        return new ApiError(HttpStatus.NOT_FOUND.value(), "无该资源: " + req.getRequestURI());
    }
 
    /**
     * 处理 HTTP 500 错误
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ApiError handleInternalServerError(HttpServletRequest req, Exception ex) {
        return new ApiError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器内部错误: " + ex.getMessage());
    }
 
    /**
     * 处理用户登录异常
     */
    @ExceptionHandler(UserLoginException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ApiError handleUserLoginException(HttpServletRequest req, UserLoginException ex) {
        return new ApiError(HttpStatus.UNAUTHORIZED.value(), "用户登录失败: " + ex.getMessage());
    }
 
    /**
     * 处理其他所有未处理的异常
     */
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ApiError handleOtherExceptions(HttpServletRequest req, Throwable ex) {
        return new ApiError(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部错误: " + ex.getMessage());
    }
}

触发异常

我们在控制器中模拟一个用户登录失败的场景,并将 UserLoginException 抛出:

@RestController
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @PostMapping("/login")
    public String login(@RequestBody LoginForm form) {
        // 验证用户名和密码
        if (!userService.validate(form.getUsername(), form.getPassword())) {
            // 用户不存在或密码错误,抛出 UserLoginException 异常
            throw new UserLoginException("用户名或密码不正确");
        }
         
        // 登录成功,返回 token 或用户信息
        // ......
    }
}

到此这篇关于SpringBoot 全局异常处理——解决404/500的文章就介绍到这了,更多相关内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/weixin_46532941/article/details/130475589

延伸 · 阅读

精彩推荐