同一异常
Posted cabinet
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同一异常相关的知识,希望对你有一定的参考价值。
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
/**
* 在controller里面内容执行之前,校验一些参数不匹配啊,Get post方法不对啊之类的
*/
@Override
protected ResponseEntity
}
@ExceptionHandler(BizException.class)
@ResponseBody
public ResultModel
e.printStackTrace();
ResultModel<String> result = new ResultModel();
result.setState(Const.FAILURE);
result.setMsgCode(e.getErrCode());
result.setMsg(e.getErrMsg());
if(e.getData()==null || e.getData().toString().trim().equals("")){
result.setData(request.getRequestURL().toString());
}else {
if (e.getData() instanceof String) {
result.setData(e.getData().toString());
}else{
result.setData(JSON.toJSONString(e.getData()));
}
}
log(e, request);
return result;
}
/**
* 请求方法中校验抛出的异常
* @param e
* @return
*/
@ExceptionHandler(ConstraintViolationException.class)
@ResponseBody
public ResultModel
//获取异常中第一个错误信息
e.printStackTrace();
ResultModel<String> result = new ResultModel();
result.setState(Const.FAILURE);
result.setMsgCode(ExceptionCode.FAILURE.getCode());
result.setMsg(e.getMessage());
result.setData(request.getRequestURL().toString());
log(e, request);
return result;
}
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResultModel
e.printStackTrace();
ResultModel<String> result = new ResultModel();
result.setState(Const.FAILURE);
result.setMsgCode(ExceptionCode.FAILURE.getCode());
result.setMsg(e.getMessage());
result.setData(request.getRequestURL().toString());
log(e, request);
return result;
}
private void log(Exception ex, HttpServletRequest request) {
logger.error("************************异常开始*******************************");
// if(getUser() != null)
// logger.error("当前用户id是" + getUser().getUserId());
logger.error(ex);
logger.error("请求地址:" + request.getRequestURL());
Enumeration enumeration = request.getParameterNames();
logger.error("请求参数");
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement().toString();
logger.error(name + "---" + request.getParameter(name));
}
StackTraceElement[] error = ex.getStackTrace();
for (StackTraceElement stackTraceElement : error) {
logger.error(stackTraceElement.toString());
}
logger.error("************************异常结束*******************************");
}
以上是关于同一异常的主要内容,如果未能解决你的问题,请参考以下文章
mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段
mvn命令异常:An error has occurred in Javadoc report generation: Unable to find javadoc command异常已解决(代码片段
springcloud报错-------关于 hystrix 的异常 FallbackDefinitionException:fallback method wasn't found(代码片段