当前位置:网站首页>备忘录 @RestControllerAdvice与异常拦截类示例
备忘录 @RestControllerAdvice与异常拦截类示例
2022-07-27 05:23:00 【sin_404】
import com.mine.ResponseMO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
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 java.util.ArrayList;
import java.util.List;
/**
* 统一异常处理基类
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 判断是否是dev环境
*/
protected boolean dev;
/**
* 全局异常
*
* @param e
* @return
*/
@ExceptionHandler(value = {Exception.class, RuntimeException.class})
public ResponseMO defaultErrorHandler(Exception e) {
logger.error(e.getMessage(), e);
String debugInfo = null;
if (dev) {
debugInfo = e.toString();
}
return ResponseMO.error("内部发生错误,请联系管理员", debugInfo);
}
/**
* 参数校验异常
*
* @param e
* @return
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class
, MissingServletRequestParameterException.class})
public ResponseMO methodArgumentNotValidException(Exception e) {
if (logger.isDebugEnabled()) {
logger.info(e.getMessage(), e);
}
//参数缺失异常
if (e instanceof MissingServletRequestParameterException) {
MissingServletRequestParameterException exception = (MissingServletRequestParameterException) e;
String message = exception.getParameterName() + "不能为空";
return ResponseMO.error(message);
}
List<ObjectError> allErrors = new ArrayList<>();
if (e instanceof BindException) {
allErrors = ((BindException) e).getBindingResult().getAllErrors();
} else if (e instanceof MethodArgumentNotValidException) {
allErrors = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors();
}
StringBuffer errors = new StringBuffer();
for (ObjectError allError : allErrors) {
errors.append(allError.getDefaultMessage());
break;
}
String debugInfo = null;
if (dev) {
debugInfo = e.toString();
}
return ResponseMO.error(errors.toString(), debugInfo);
}
/**
* 请求方法不支持异常
*
* @param e
* @return
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseMO methodNotSupported(HttpRequestMethodNotSupportedException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
String message = "不支持" + e.getMethod() + "请求访问";
String debugInfo = null;
if (dev) {
debugInfo = e.toString();
}
return ResponseMO.error(message, debugInfo);
}
/**
* 请求内容不支持
*
* @param e
* @return
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseMO httpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
String message = "不支持'" + e.getContentType() + "'内容";
String debugInfo = null;
if (dev) {
debugInfo = e.toString();
}
return ResponseMO.error(message, debugInfo);
}
}
边栏推荐
- selenium知识点
- shell--条件语句(if语句、case语句)
- FTP服务简介与配置
- Converting ArcGIS style stylesheet files to GeoServer SLD files
- Programming learning records - Lesson 5 [branch and loop statements]
- 反射器中getattr,hasattr,delattr,setattr的使用
- This is my blog
- bug分类及缺陷和csv文件测试
- Solve the problems of CONDA install stop and interruption
- LVM与磁盘配额
猜你喜欢
随机推荐
Shell -- operation of variables
selenium知识点
Database commands
源码编译安装LNMP和DISCUZ论坛
KVM命令集管理虚拟机
Interface test process and interview questions
FTP服务简介与配置
PXE高效批量网络装机
C language -- string operation function and memory operation function
二叉树——搜索树
LVM与磁盘配额
Path to file
logging日志的封装
Concept and principle of DHCP
如何规范式编写yaml文件
Seven sorting details
Network troubleshooting: Ping and tracert commands
PXE efficient batch network installation
项目实训经历2
七大排序详解







