当前位置:网站首页>Memo @restcontrolleradvice and exception interception class example
Memo @restcontrolleradvice and exception interception class example
2022-07-27 06:48:00 【sin_ four hundred and four】
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;
/**
* Unified exception handling base class
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Judge whether it is dev Environmental Science
*/
protected boolean dev;
/**
* Global exception
*
* @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(" There was an internal error , Please contact the Administrator ", debugInfo);
}
/**
* Parameter verification exception
*
* @param e
* @return
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class
, MissingServletRequestParameterException.class})
public ResponseMO methodArgumentNotValidException(Exception e) {
if (logger.isDebugEnabled()) {
logger.info(e.getMessage(), e);
}
// Parameter missing exception
if (e instanceof MissingServletRequestParameterException) {
MissingServletRequestParameterException exception = (MissingServletRequestParameterException) e;
String message = exception.getParameterName() + " Can't be empty ";
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);
}
/**
* The request method does not support exception
*
* @param e
* @return
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseMO methodNotSupported(HttpRequestMethodNotSupportedException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
String message = " I won't support it " + e.getMethod() + " Request access ";
String debugInfo = null;
if (dev) {
debugInfo = e.toString();
}
return ResponseMO.error(message, debugInfo);
}
/**
* The requested content does not support
*
* @param e
* @return
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseMO httpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
String message = " I won't support it '" + e.getContentType() + "' Content ";
String debugInfo = null;
if (dev) {
debugInfo = e.toString();
}
return ResponseMO.error(message, debugInfo);
}
}
边栏推荐
- 阿里云短信验证第三方接口(快速使用)
- What is special about the rehabilitation orthopedic branch of 3D printing brand?
- Vscode solves the problem of using stuck ipynb files when running
- Cesium tutorial (1) interface introduction -3dfiles loading - change mouse operation settings
- 2022年全球6家最具技术实力的的智能合约审计公司盘点
- Lamp -- source code compilation and installation
- Project training experience 1
- Talk about why you need to declare the member function of a class as private
- shell函数
- Shell programming specification and redirection and pipeline operation
猜你喜欢

云原生运行环境搭建

Redis快速学习

torch加载自定义模型的问题

pycharm在虚拟环境下跑jupyter notebook问题记录

2022上半年英特尔有哪些“硬核创新”?看这张图就知道了!

如何避免漏洞?向日葵远程为你讲解不同场景下的安全使用方法

logging日志的封装

FTX Foundation funded 15million to help covid-19 clinical trials, which will affect global public health

Many of the world's top 500 enterprises gathered at the second digital Expo, and the digital industry curtain is about to open!

Redis' idea under windows is not connected
随机推荐
关于过快S验证码的一些问题
shell--变量的运算
When a subclass calls the constructor of its parent class
DNS domain name resolution service
向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解清楚再判断向日葵:遇到电脑漏洞别担心,了解
Shell script delete automatically clean up files that exceed the size
Talk about why you need to declare the member function of a class as private
Common font and color settings of markdown documents
DNS域名解析服务
Use of getattr, hasattr, delattr and setattr in reflectors
Source code compilation and installation LNMP and discuz Forum
Many of the world's top 500 enterprises gathered at the second digital Expo, and the digital industry curtain is about to open!
Raid explanation and configuration
多模态数据库 | 星环科技多模数据库ArgoDB“一库多用“,构建高性能湖仓集一体平台
NFS简介和配置
Shell programming specification and redirection and pipeline operation
ESXI虚拟机启动,模块“MonitorLoop”打开电源失败
System security and Application
FTX 基金会资助1500万帮助新冠疫苗临床实验,将影响全球公共卫生
Kaggle calls the custom module method