- 如果是dubbo服务 直接在对外暴露服务的哪个模块添加该类即可
- 如果是springboot微服务 我们可以把他配置到一个基础工程里 这个工程没有启动类 提供公用的util jar包等等 也不需要注册到注册中心
- 并且在其他项目的启动类添加如下配置 我在网关配置的
- 目的是为了让服务知道该配置 否则不生效
- ApiReturnObject是一个统一返回值类型 可以根据自己喜好进行定义 这里就不贴代码了
package com.softdev.config;
import com.softdev.common.config.BusinessException;
import com.softdev.common.config.CommonErrorCode;
import com.softdev.common.config.ErrorCode;
import com.softdev.common.utils.ApiReturnObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ApiReturnObject processExcetion(HttpServletRequest request,
HttpServletResponse response,
Exception e){
if(e instanceof BusinessException){
LOGGER.info(e.getMessage(),e);
BusinessException businessException= (BusinessException) e;
ErrorCode errorCode = businessException.getErrorCode();
int code = errorCode.getCode();
String desc = errorCode.getDesc();
return new ApiReturnObject(String.valueOf(code),desc,"");
}
LOGGER.error("系统异常:",e);
return new ApiReturnObject(String.valueOf(CommonErrorCode.UNKOWN.getCode()),CommonErrorCode.UNKOWN.getDesc());
}
}
- 在如上有了全局处理器后 我们可以自定义一个异常类和枚举类
- 自定义异常类是可预判的 比如前端传参不正确等等可以抛出自定义异常
package com.softdev.common.config;
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 5565760508056698922L;
private ErrorCode errorCode;
public BusinessException(ErrorCode errorCode) {
super();
this.errorCode = errorCode;
}
public BusinessException() {
super();
}
public BusinessException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
}
public BusinessException(ErrorCode errorCode, String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
this.errorCode = errorCode;
}
public BusinessException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
public BusinessException(ErrorCode errorCode, String arg0, Throwable arg1) {
super(arg0, arg1);
this.errorCode = errorCode;
}
public BusinessException(String arg0) {
super(arg0);
}
public BusinessException(ErrorCode errorCode, String arg0) {
super(arg0);
this.errorCode = errorCode;
}
public BusinessException(Throwable arg0) {
super(arg0);
}
public BusinessException(ErrorCode errorCode, Throwable arg0) {
super(arg0);
this.errorCode = errorCode;
}
public ErrorCode getErrorCode() {
return errorCode;
}
public void setErrorCode(ErrorCode errorCode) {
this.errorCode = errorCode;
}
}
package com.softdev.common.config;
public enum CommonErrorCode implements ErrorCode {
E_100101(100101,"传入参数与接口不匹配"),
E_100102(100102,"验证码错误"),
E_100103(100103,"验证码为空"),
E_100104(100104,"查询结果为空"),
E_100105(100105,"ID格式不正确或超出Long存储范围"),
E_100106(100106,"上传错误"),
E_100107(100107,"发送验证码错误"),
E_100108(100108,"入参不全"),
E_999991(999991,"调用微服务-授权服务 被熔断"),
E_999992(999992,"调用微服务-用户服务 被熔断"),
E_999993(999993,"调用微服务-资源服务 被熔断"),
E_999994(999994,"调用微服务-同步服务 被熔断"),
E_999910(999910,"调用微服务-没有传tenantId租户Id"),
E_999911(999911,"调用微服务-没有json-token令牌"),
E_999912(999912,"调用微服务-json-token令牌解析有误"),
E_999913(999913,"调用微服务-json-token令牌有误-没有当前租户信息"),
E_999914(999914,"调用微服务-json-token令牌有误-该租户下没有权限信息"),
E_NO_AUTHORITY(999997,"没有访问权限"),
CUSTOM(999998,"自定义异常"),
UNKOWN(999999,"未知错误");
private int code;
private String desc;
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
private CommonErrorCode(int code, String desc) {
this.code = code;
this.desc = desc;
}
public static CommonErrorCode setErrorCode(int code) {
for (CommonErrorCode errorCode : CommonErrorCode.values()) {
if (errorCode.getCode()==code) {
return errorCode;
}
}
return null;
}
}
package com.softdev.common.config;
public interface ErrorCode {
int getCode();
String getDesc();
}