SpringBoot @ControllerAdvice @ExceptionHandler 的使用详解

叙述

顾名思义,@ControllerAdvice 就是 @Controller 的增强版。@ControllerAdvice 主要用来处理全局数据,一般搭配 @ExceptionHandler、@ModelAttribute、@InitBinder 使用。下面分别进行介绍。

教程

@ExceptionHandler

@ControllerAdvice 最常见的使用场景就是全局异常处理。假设我们项目中有个文件上传功能,并且对文件上传的大小限制进行了配置。

如果用户上传的文件超过了限制大小,就会抛出异常,此时可以通过 @ControllerAdvice 结合 @ExceptionHandler 定义全局异常捕获机制,具体代码如下:

代码说明:

  • CustomExceptionHandler 类上面添加了 @ControllerAdvice 注解。当系统启动时,该类就会被扫描到 Spring 容器中。
  • uploadException 方法上面添加了 @ExceptionHandler 注解,其中定义的 MaxUploadSizeExceededException.class 表明该方法用来处理 MaxUploadSizeExceededException 类型的异常。如果想让该方法处理所有类型的异常,只需要将 MaxUploadSizeExceededException 改成 Exception 即可。
  • 异常处理方法的参数可以有异常实例、HttpServletResponse 以及 HttpServletRequest、Model 等。 异常处理方法的返回值可以是一段 JSON、一个 ModelAndView、一个逻辑视图名等。
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
 
@ControllerAdvice
public class CustomExceptionHandler {
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void uploadException(MaxUploadSizeExceededException e, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write("上传文件大小超出限制!");
        out.flush();
        out.close();
    }
}

这时当我们上传一个超大文件,页面上就会显示一个错误提示。

原文:SpringBoot - @ControllerAdvice的使用详解1(全局异常处理 @ExceptionHandler)


下面使用一个返回参数是 ModelAndView 的样例。这里我们使用的页面模版为 Thymeleaf,所以首先要在 pom.xml 中添加相关依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
@ControllerAdvice
public class CustomExceptionHandler {
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView uploadException(MaxUploadSizeExceededException e, HttpServletResponse resp)
            throws IOException {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "上传文件超出限制!");
        mv.setViewName("error");
        return mv;
    }
}

然后在 resources/templates 目录下创建 error.html 文件,内容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div th:text="${msg}"></div>
</body>
</html>

同样上传一个超大文件,页面上就会显示一个错误提示:

原文:SpringBoot - @ControllerAdvice的使用详解1(全局异常处理 @ExceptionHandler)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值