@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>