springboot 全局异常处理ControllerAdvice

112 篇文章 0 订阅

@ControllerAdvice

顾名思义,@ControllerAdvice就是@Controller的增强版。

@ControllerAdvice主要用来处理全局数据,一般搭配@ExceptionHandler、@ModelAttribute以及@InitBinder使用。

全局异常处理

@ControllerAdvice最常见的使用场景就是全局异常处理。

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

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void uploadException(MaxUploadSizeExceededException e, HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("文件上传超出限制");
        writer.flush();
        writer.close();
    }

只需在系统中定义CustomExceptionHandler类,然后添加@ControllerAdvice注解即可。

当系统启动时,该类就会被扫描到Spring容器中,然后定义uploadException方法,在该方法上添加了@ExceptionHandler注解,其中定义的MaxUploadSizeExceededException.class表明该方法用来处理MaxUploadSizeExceededException类型的异常。

如果想让该方法处理所有类型的异常,只需将MaxUploadSizeExceededException改为Exception即可。

方法的参数可以有异常实例、HttpServletResponse以及HttpServletRequest、Model等,返回值可以是一段JSON、一个ModelAndView、一个逻辑视图名等。

如果返回参数是一个ModelAndView,假设使用的页面模板为Thymeleaf(注意添加Thymeleaf相关依赖),此时异常处理方法定义如下:

 CustomerExceptionHandler.java

package com.shrimpking.error;

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;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;


/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/4 16:35
 */
@ControllerAdvice
public class CustomExceptionHandler
{
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView uploadException(MaxUploadSizeExceededException e) throws IOException
    {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","上传文件大小超出限制");
        mv.setViewName("error");
        return mv;
    }

//    @ExceptionHandler(MaxUploadSizeExceededException.class)
//    public void uploadException(MaxUploadSizeExceededException e, HttpServletResponse response)
//    {
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter writer = response.getWriter();
//        writer.write("文件上传超出限制");
//        writer.flush();
//        writer.close();
//    }
}

FileUploadController.java

package com.shrimpking.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2023/6/4 16:42
 */
@RestController
public class FileUploadController
{
    /**
     * 格式化日期
     */
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest request)
    {
        //获取文件路径
        String realPath = request.getSession().getServletContext()
                .getRealPath("/uploadFile/");
        //按日期格式化字符串
        String format = sdf.format(new Date());
        //创建文件对象
        File folder = new File(realPath + format);
        if(!folder.isDirectory())
        {
            //创建目录
            folder.mkdirs();
        }

        //获取上传文件的名称
        String oldName = uploadFile.getOriginalFilename();
        //根据uuid,生成新文件名称
        String newName = UUID.randomUUID().toString() +
                oldName.substring(oldName.lastIndexOf("."),oldName.length());

        try
        {
            //上传的临时文件,搬运至
            uploadFile.transferTo(new File(folder,newName));
            //获取新文件的全路径名称
            String filePath = request.getScheme() + "://"
                    + request.getServerName() + ":"
                    + request.getServerPort() + "/uploadFile/"
                    + format + newName;
            return filePath;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return "上传失败";
    }
}

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>upload</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadFile" id="uploadFile" value="上传文件">
    <input type="submit" value="上传">
</form>
</body>
</html>

error.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>error</title>
</head>
<body>
    <h3>错误提示</h3>
    <div th:text="${msg}"></div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值