springboot2.2x版本项目中配置全局异常处理
作者:互联网
说明
- AjaxException.class 为项目自定义的一个运行时异常类,若项目中未定义专用的异常类型,则可删除此异常处理方法;
- 在springboot的启动类需要配置@ComponentScan(basePackages = {“异常类的包路径”}),相对路径或绝对路径都可,否则异常处理类不生效;
核心异常处理类
package com.spin.common.multi.error;
import com.spin.common.base.ApiResult;
import com.spin.common.enums.ResponseEnum;
import com.spin.common.exceptions.AjaxException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestControllerAdvice
@Slf4j
public class BusinessExceptionHandler {
/**
* 自定义异常类处理方法
*/
@ExceptionHandler(AjaxException.class)
public ApiResult<String> customExceptionHandler(HttpServletRequest request, AjaxException ex){
ApiResult<String> apiResult=new ApiResult<>();
apiResult.setMessage(ex.getMessage());
apiResult.setSuccess(false);
apiResult.setErrCode(String.valueOf(ex.getCode()));
log.error("全局异常处理输入错误信息,请求路径:-------->{}",request.getRequestURL());
log.error("全局异常处理输入错误信息,error:-------->",ex);
return apiResult;
}
/**
* 通用的接口映射异常处理方
*/
@ExceptionHandler(Exception.class)
protected ApiResult<String> commonExceptionHandler(HttpServletRequest request,Exception ex) {
ApiResult<String> apiResult =new ApiResult<>();
if (ex instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
//多个错误,取第一个
FieldError error = fieldErrors.get(0);
String msg = error.getDefaultMessage();
apiResult.setMessage(msg);
apiResult.setSuccess(false);
apiResult.setErrCode(ResponseEnum.RESPONSE_100000002.getCode().toString());
}else if (ex instanceof MethodArgumentTypeMismatchException) {
MethodArgumentTypeMismatchException exception = (MethodArgumentTypeMismatchException) ex;
log.error("参数转换失败,方法:" + exception.getParameter().getMethod().getName() + ",参数:" + exception.getName()
+ ",信息:" + exception.getLocalizedMessage());
apiResult.setMessage(ResponseEnum.RESPONSE_402.getName());
apiResult.setSuccess(false);
apiResult.setErrCode(ResponseEnum.RESPONSE_402.getCode().toString());
}else {
apiResult.setErrCode(ResponseEnum.RESPONSE_500.getCode().toString());
apiResult.setSuccess(false);
apiResult.setMessage(ResponseEnum.RESPONSE_500.getName());
}
log.error("全局异常处理输入错误信息,请求路径:-------->{}",request.getRequestURL());
log.error("全局异常处理输入错误信息,error:-------->",ex);
return apiResult;
}
}
状态码枚举类
package com.spin.common.enums;
/**
* @author OuYang Li
* @version 1.0.0
* @ClassName ResponseEnum.java
* @Description 响应对象 统一消息管理
* @createTime 2021-01-22 16:47:00
*/
public enum ResponseEnum {
/*** code尾数为1 则前端不能弹出提示框, code尾数为2,则前端可以弹出提示框***/
/*** code前四位为业务组,第5-8位为业务组中的子状态码 第9位为前端的弹窗的验证码***/
/*** 例110000002 1100表示员工业务,0000表示员工代码已存在,2表示前端需要弹窗***/
/***----------------常规响应码200-999------------------------begin***/
RESPONSE_200(200, "请求成功"),
RESPONSE_400(400, "参数异常"),
RESPONSE_401(401, "用户未授权"),
RESPONSE_402(402, "参数类型转换异常"),
RESPONSE_500(500, "服务器未知异常"),
/***----------------常规响应码200-999------------------------end***/
/***----------------业务响应码定义为九位数------------------------begin***/
RESPONSE_100000002(1000000002,"JSR303校验错误"),
/***----------------员工业务响应码-----------------------begin***/
RESPONSE_110000002(110000002,"工号已存在,请重新输入"),
RESPONSE_110000012(110000012,"账号或者密码错误"),
RESPONSE_110000022(110000022,"验证码错误请重新输入"),
RESPONSE_110000032(110000032,"修改员工账户密码失败"),
RESPONSE_110000042(110000042,"多个部门需设置主部门信息,请重新设置!"),
RESPONSE_110000052(110000052,"不允许多个机构为主机构!"),
RESPONSE_110000062(110000062,"批量删除员工失败!"),
/***----------------机构业务响应码------------------------begin***/
RESPONSE_110100002(110100002,"机构下存在下级机构,请先删除下级机构!"),
RESPONSE_110100012(110100012,"机构代码已存在,请重新录入"),
RESPONSE_110100022(110100022,"机构下存在员工,请先删除员工!"),
/***----------------系统参数服务响应码------------------------begin***/
RESPONSE_110300001(110300001,"获取企业corpId失败"),
RESPONSE_110300011(110300011,"获取企业callbackExternalContactToken失败"),
RESPONSE_110300021(110300021,"获取企业encodingAESKey失败"),
RESPONSE_110300031(110300031,"获取企业contactSecret失败"),
RESPONSE_110300041(110300041,"获取企业AddressCorpsecret失败"),
RESPONSE_110300002(110300002,"存在相同的参数键名"),
RESPONSE_110300051(110300051,"找不到相对应的键值"),
RESPONSE_110300061(110300061,"调用获取参数键值接口失败"),
/***----------------企业微信服务响应码------------------------begin***/
RESPONSE_110400001(110400001,"获取的assess_token失败"),
RESPONSE_110400011(110400011,"获取企业微信部门列表失败"),
RESPONSE_110400021(110400021,"获取企业微信成员详情失败"),
RESPONSE_110400031(110400031,"获取企业微信客户群列表失败"),
RESPONSE_110400041(110400041,"获取企业微信客户群详情信息失败"),
RESPONSE_110400051(110400051,"获取企业微信客户列表失败"),
RESPONSE_110400061(110400061,"获取企业微信客户详情信息失败"),
RESPONSE_110400071(110400071,"企业微信回调对象为空"),
RESPONSE_110400081(110400081,"获取企业微信配置了客户联系功能的成员列表失败"),
RESPONSE_110400091(110400091,"新增客户群信息失败"),
RESPONSE_110400101(110400101,"新增外部联系人信息失败"),
RESPONSE_110400111(110400111,"获取解密的消息明文异常"),
RESPONSE_110400121(110400121,"拉取聊天记录异常"),
RESPONSE_110400131(110400131,"拉取媒体文件异常"),
RESPONSE_110400141(110400141,"获取内部群信息失败"),
/***--------------------角色业务响应码------------------------begin***/
RESPONSE_110500002(110500002,"角色编码已存在,请重新输入!"),
/***--------------------应用业务响应码------------------------begin***/
RESPONSE_110600002(110600002,"应用代码已存在,请重新录入!"),
/***--------------------业务字典响应码------------------------begin***/
RESPONSE_110700002(110700002,"类型代码已存在"),
RESPONSE_110700012(110700012,"删除字典业务类型信息失败,请先删除业务字典类型下的业务字典项信息"),
RESPONSE_110700022(110700022,"新增字典项代码重复!"),
RESPONSE_110700032(110700032,"批量删除的参数列表为空"),
/***--------------------索引库操作响应码------------------------begin***/
RESPONSE_110800001(110800001,"会话消息存入索引库失败"),
RESPONSE_110800011(110800011,"更新索引库setting配置异常"),
RESPONSE_110800021(110800021,"查询索引库setting配置异常"),
RESPONSE_110800031(110800031,"ES索引库更新文档异常"),
RESPONSE_110800041(110800041,"ES索引库批量更新文档异常"),
RESPONSE_110800051(110800051,"ES索引库删除文档异常"),
RESPONSE_110800061(110800061,"创建索引库mapping配置异常"),
RESPONSE_110800071(110800071,"删除索引库异常"),
RESPONSE_110800081(110800081,"创建索引库异常"),
RESPONSE_110800091(110800091,"索引库查询文档数据异常"),
/***--------------------客户群操作响应码------------------------begin***/
RESPONSE_110900002(110900002,"外部联系人同步未完成!"),
RESPONSE_110900012(110900012,"外部联系人未开始同步!"),
/***--------------------定时任务信息操作响应码------------------------begin***/
RESPONSE_111000002(111000002,"删除定时任务调度,删除任务失败,请联系系统管理员!!"),
;
private Integer code;
private String name;
ResponseEnum(Integer code, String name) {
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
public String getName() {
return name;
}
public static String getTextByKey(String pKey){
ResponseEnum[] payEnums = ResponseEnum.values();
for (ResponseEnum enumObj: payEnums) {
if(enumObj.getCode().equals(pKey)){
return enumObj.getName();
}
}
return pKey;
}
public static Integer getKeyByText(String pText){
ResponseEnum[] comEnums = ResponseEnum.values();
for (ResponseEnum enumObj: comEnums) {
if(enumObj.getName().equals(pText)){
return enumObj.getCode();
}
}
return null;
}
}
标签:------------------------,springboot2.2,apiResult,失败,版本,全局,异常,RESPONSE,ResponseEn 来源: https://blog.csdn.net/ouyangli2011/article/details/115001828