当前位置:网站首页>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);
}
}
边栏推荐
- For redis under windows, it can only read but not write
- Common font and color settings of markdown documents
- Explanation of server related indicators
- Kaggle calls the custom module method
- What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
- Install redis under Windows
- What if the website server is attacked? Sunflower tips that preventing loopholes is the key
- 创建一个不依赖于任何基础镜像的容器
- Shell -- custom variables and assignments
- Shell sentence judgment exercise
猜你喜欢
随机推荐
FTX 基金会资助1500万帮助新冠疫苗临床实验,将影响全球公共卫生
Soul持续发力社交渠道赴港上市,“Soul式社交”凭什么火出圈?
What is special about the rehabilitation orthopedic branch of 3D printing brand?
How to manage a large number of scheduled tasks
Linux安装与卸载MySql
FTP service introduction and configuration
Detection and identification data set and yolov5 model of helmet reflective clothing
QGIS series (1) -qgis (server APACHE) win10 installation
Ancient art - make good use of long tail keywords
Linu performance tuning: how can we alleviate the situation in the face of DDoS attacks?
Use -wall to clear code hidden dangers
keras-ocr实例测试
网站服务器被攻击怎么办?向日葵提示防范漏洞是关键
Shell脚本一键配置LAMP
PSI|CSI和ROC|AUC和KS -备忘录
Shell sentence judgment exercise
goLang的一个跨域问题
Linux安装Redis操作
ES6新特性(入门)
向日葵教大家如何防范拒绝服务攻击漏洞?









