当前位置:网站首页>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);
}
}
边栏推荐
- Basic knowledge of English: Rules for the use of non predicates Part 1
- Redis' idea under windows is not connected
- Detection and identification data set and yolov5 model of helmet reflective clothing
- System security and Application
- Shell script one click configuration lamp
- px4源码编译之 建立自己的程序模块
- Shell script loop
- where接自定义函数导致查询缓慢
- iptables防火墙
- Project training experience 2
猜你喜欢

torch加载自定义模型的问题

DNS域名解析服务

PXE efficient batch network installation

3D打印品牌的康复骨科支具有何特别之处?

PSI|CSI和ROC|AUC和KS -备忘录

If conditional statement of shell

Iptables firewall, SNAT and DNAT

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

What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!

如何避免漏洞?向日葵远程为你讲解不同场景下的安全使用方法
随机推荐
Explanation of server related indicators
shell--变量的运算
darknet-yolov3、yolo-fastect使用rtx30系显卡cuda环境在win10平台编译与训练的相关问题
Sunflower teaches you how to prevent denial of service attacks?
GoLand writes Go program
shell的编程规范and重定向与管道操作
如何避免漏洞?向日葵远程为你讲解不同场景下的安全使用方法
Go language learning
向日葵教大家如何防范拒绝服务攻击漏洞?
装饰器函数与类装饰器的使用
What if the website server is attacked? Sunflower tips that preventing loopholes is the key
Summary of frequently asked questions in the interview [summarized after painstaking work all night]
DNS域名解析服务
Ancient art - make good use of long tail keywords
Redis快速学习
Detection and identification data set and yolov5 model of helmet reflective clothing
Soul持续发力社交渠道赴港上市,“Soul式社交”凭什么火出圈?
FTX US推出FTX Stocks,向主流金融行业迈进
ArcGIS for JS API (2) get the ID set of element services
How to write yaml file in a standard way