1、返回异常接收页面
一、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
二、添加配置
spring:
thymeleaf:
cache: false
suffix: .html
encoding: UTF-8
servlet:
content-type: text/html
三、templates下新建error.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>全局异常处理</title>
</head>
<body>
<p th:text="${'错误路径:' + url}"/>
<p th:text="${'错误信息:' + msg}"/>
</body>
</html>
四、 定义统一异常接收
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value=Exception.class)//处理哪一类异常
public ModelAndView handlerException(Exception e, HttpServletRequest request){
ModelAndView modelAndView = new ModelAndView();
//跳转异常页面路径
modelAndView.setViewName("error");
//页面显示错误信息 页面只需要使用对应的取值方式取值就可以取到msg了
modelAndView.addObject("msg",e.getMessage());
modelAndView.addObject("url", request.getRequestURL());
return modelAndView;
}
}
2、返回json数据格式
一、统一异常处理
/**
* 全局异常处理
* 自定义全局异常处理除了可以处理上述的数据格式之外,也可以处理页面的跳转,
* 只需在新增的异常方法的返回处理上填写该跳转的路径并不使用ResponseBody 注解即可。
* @author coco
* @date 2021/10/14
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理自定义的业务异常
* @param ex
* @return
*/
@ExceptionHandler(value = BizException.class)
@ResponseBody
public Result<?> bizExceptionHandler(BizException ex){
if(log.isErrorEnabled()){
log.error("发生业务异常!原因是:{}",ex.getErrorMsg());
}
return Result.error(ex.getErrorCode(),ex.getErrorMsg());
}
/**
* 处理空指针的异常
* @param ex
* @return
*/
@ExceptionHandler(value =NullPointerException.class)
@ResponseBody
public Result<?> exceptionHandler(NullPointerException ex){
if(log.isErrorEnabled()){
log.error("发生空指针异常!原因是:",ex);
}
return Result.error(ResultCodeEnum.BODY_NOT_MATCH);
}
/**
* 处理其他异常
* @param ex
* @return
*/
@ExceptionHandler(value =Exception.class)
@ResponseBody
public Result<?> exceptionHandler(Exception ex){
if(log.isErrorEnabled()){
log.error("未知异常!原因是:",ex);
}
return Result.error(ResultCodeEnum.INTERNAL_SERVER_ERROR);
}
/**
* 处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。
* @param ex
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public Result<?> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
String message = ex.getBindingResult()
.getAllErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining());
return Result.fail(message);
}
/**
* 处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
* @param ex
* @return
*/
@ExceptionHandler({ConstraintViolationException.class})
public Result<?> handleConstraintViolationException(ConstraintViolationException ex) {
String message = ex.getConstraintViolations()
.stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.joining());
return Result.fail(message);
}
/**
* 处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常,详情继续往下看代码
* @param ex
* @return
*/
@ExceptionHandler(BindException.class)
@ResponseBody
public Result<?> bindExceptionHandler(BindException ex) {
String message = ex.getBindingResult()
.getAllErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining());
return Result.fail(message);
}
}