package com.qiangesoft.ma.exception;
import com.qiangesoft.ma.util.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.List;
import java.util.Set;
/**
* 全局错误处理器
*
* @author qiangesoft
* @date 2024-07-17
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {
/**
* 请求参数缺失
*
* <p>
* 接口上设置了@RequestParam("xx")参数,结果并未传递xx
*/
@ExceptionHandler(value = MissingServletRequestParameterException.class)
public ResultVO missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
log.warn("[missingServletRequestParameterExceptionHandler]", e);
return ResultVO.fail(CommonExceptionEnum.BAD_REQUEST.getCode(), String.format("请求参数缺失:%s", e.getParameterName()));
}
/**
* 请求参数类型错误
*
* <p>
* 接口上设置了@RequestParam("xx")参数为Integer,结果传递xx类型为String
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResultVO methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException e) {
log.warn("[methodArgumentTypeMismatchExceptionHandler]", e);
return ResultVO.fail(CommonExceptionEnum.BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", e.getMessage()));
}
/**
* 参数校验异常
*
* @param e
* @return
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
public ResultVO methodArgumentNotValidExceptionHandler(Exception e) {
log.error("[methodArgumentNotValidExceptionHandler]", e);
List<FieldError> fieldErrors = null;
// 参数校验不正确
if (e instanceof MethodArgumentNotValidException) {
fieldErrors = ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors();
}
// 参数绑定不正确
if (e instanceof BindException) {
fieldErrors = ((BindException) e).getBindingResult().getFieldErrors();
}
if (fieldErrors == null) {
return ResultVO.fail(CommonExceptionEnum.METHOD_ARGUMENT_NOT_VALID);
}
StringBuilder stringBuilder = new StringBuilder();
for (FieldError fieldError : fieldErrors) {
stringBuilder.append(fieldError.getField() + ":" + fieldError.getDefaultMessage()).append(";");
}
return ResultVO.fail(CommonExceptionEnum.METHOD_ARGUMENT_NOT_VALID, stringBuilder.toString());
}
/**
* 参数校验异常
*
* @param e
* @return
*/
@ExceptionHandler({ConstraintViolationException.class, ValidationException.class})
public ResultVO validationExceptionHandler(Exception e) {
log.error("[validationExceptionHandler]", e);
// Validator校验不通过产生的异常
StringBuilder stringBuilder = new StringBuilder();
if (e instanceof ConstraintViolationException) {
Set<ConstraintViolation<?>> constraintViolations = ((ConstraintViolationException) e).getConstraintViolations();
for (ConstraintViolation<?> constraintViolation : constraintViolations) {
stringBuilder.append(constraintViolation.getMessage()).append(";");
}
}
// 本地参数校验时,抛出的ValidationException异常
if (e instanceof ValidationException) {
stringBuilder.append(e.getMessage());
}
return ResultVO.fail(CommonExceptionEnum.METHOD_ARGUMENT_NOT_VALID, stringBuilder.toString());
}
/**
* 请求地址不存在
*
* <p>
* 注意,它需要设置如下两个配置项:
* 1. spring.mvc.throw-exception-if-no-handler-found 为 true
* 2. spring.mvc.static-path-pattern 为 /statics/**
*/
@ExceptionHandler(NoHandlerFoundException.class)
public ResultVO noHandlerFoundExceptionHandler(NoHandlerFoundException e) {
log.warn("[noHandlerFoundExceptionHandler]", e);
return ResultVO.fail(CommonExceptionEnum.NOT_FOUND);
}
/**
* 请求方法不正确
*
* <p>
* 接口的方法为GET方式,结果请求方法为POST方式,导致不匹配
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResultVO httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e) {
log.warn("[httpRequestMethodNotSupportedExceptionHandler]", e);
return ResultVO.fail(CommonExceptionEnum.METHOD_NOT_ALLOWED);
}
/**
* 消息不可重复读异常
*
* @param e
* @return
*/
@ExceptionHandler({HttpMessageNotReadableException.class})
public ResultVO methodArgumentNotValidExceptionHandler(HttpMessageNotReadableException e) {
log.error("[methodArgumentNotValidExceptionHandler]", e);
return ResultVO.fail(CommonExceptionEnum.HTTP_MESSAGE_NOT_READABLE);
}
/**
* 业务异常
*
* @param e
* @return
*/
@ExceptionHandler(ServiceException.class)
public ResultVO serviceExceptionHandler(ServiceException e) {
log.error("[serviceExceptionHandler]", e);
return ResultVO.fail(e.getCode(), e.getErrorMessage());
}
/**
* 兜底异常
*
* @param e
* @return
*/
@ExceptionHandler(Throwable.class)
public ResultVO exceptionHandler(Throwable e) {
log.error("[exceptionHandler]", e);
return ResultVO.fail(CommonExceptionEnum.FAILED);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
wechat-ma.zip (32个子文件)
wechat-ma
pom.xml 5KB
src
main
resources
mapper
SysUserMapper.xml 223B
application.yml 3KB
logback-spring.xml 4KB
java
com
qiangesoft
ma
WxMaApplication.java 495B
mapper
SysUserMapper.java 306B
controller
AuthController.java 3KB
pojo
dto
UserLoginDTO.java 950B
vo
UserLoginVO.java 1KB
service
ISysUserService.java 801B
impl
SysUserServiceImpl.java 2KB
enums
StorageType.java 237B
HttpClientType.java 308B
UserStatusEnum.java 880B
entity
SysUser.java 2KB
exception
AbstractExceptionEnum.java 223B
CommonExceptionEnum.java 852B
GlobalExceptionAdvice.java 6KB
ServiceException.java 654B
properties
WxMaProperties.java 2KB
util
PageResult.java 351B
BasePage.java 487B
ResultVO.java 2KB
RsaUtil.java 4KB
config
ma
store
AbstractWxMaStorageConfiguration.java 2KB
RedisWxMaStorageConfiguration.java 2KB
MemoryWxMaStorageConfiguration.java 1KB
WxMaAutoConfiguration.java 565B
WxMaStorageAutoConfiguration.java 559B
WxMaServiceAutoConfiguration.java 2KB
CustomMetaObjectHandler.java 958B
MybatisPlusConfig.java 816B
共 32 条
- 1
资源评论
PG_强哥
- 粉丝: 1134
- 资源: 40
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功