exception结构:
定义全局异常处理类
package com.zjm.servicebase.exceptionhandler;
import com.zjm.commonutils.util.ExceptionUtil;
import com.zjm.commonutils.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
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.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public R error(Exception e){
e.printStackTrace();
return R.error().message("有异常");
}
//特定异常 先找特定异常 再找全局异常
@ExceptionHandler(ArithmeticException.class)
public R error(ArithmeticException e) {
e.printStackTrace();
return R.error().message("执行了ArithmeticException异常处理..");
}
@ExceptionHandler(value= MethodArgumentNotValidException.class)
public R handleVaildException(MethodArgumentNotValidException e){
log.error("数据校验出现问题{},异常类型:{}",e.getMessage(),e.getClass());
BindingResult bindingResult = e.getBindingResult();
Map<String,String> errorMap = new HashMap<>();
bindingResult.getFieldErrors().forEach((fieldError)->{
errorMap.put(fieldError.getField(),fieldError.getDefaultMessage());
});
return R.error().code(BizCodeEnume.VAILD_EXCEPTION.getCode()).message(BizCodeEnume.VAILD_EXCEPTION.getMsg()).data("data",errorMap);
}
//自定义异常
@ExceptionHandler(MSException.class)
public R error(MSException e) {
log.error(e.getMessage());
log.error(ExceptionUtil.getMessage(e));
e.printStackTrace();
return R.error().code(e.getCode()).message(e.getMsg());
}
}
自定义异常处理类,在全局异常处理中引用它
package com.zjm.servicebase.exceptionhandler;
import com.zjm.commonutils.result.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//自定义异常
@Data
@AllArgsConstructor //生成有参数构造方法
@NoArgsConstructor //生成无参数构造
public class MSException extends RuntimeException {
private Integer code;//状态码
private String msg;//异常信息
public MSException(String message, Integer code) {
super(message);
this.code = code;
}
public MSException(ResultCodeEnum resultCodeEnum) {
super(resultCodeEnum.getMessage());
this.code = resultCodeEnum.getCode();
}
@Override
public String toString() {
return "MSException{" +
"message=" + this.getMessage() +
", code=" + code +
'}';
}
}
定义一个枚举
package com.zjm.servicebase.exceptionhandler;
/***
* 错误码和错误信息定义类
* 1. 错误码定义规则为5为数字
* 2. 前两位表示业务场景,最后三位表示错误码。例如:100001。10:通用 001:系统未知异常
* 3. 维护错误码后需要维护错误描述,将他们定义为枚举形式
* 错误码列表:
* 10: 通用
* 001:参数格式校验
* 11: 商品
* 12: 订单
* 13: 购物车
* 14: 物流
*
*
*/
public enum BizCodeEnume {
UNKNOW_EXCEPTION(10000,"系统未知异常"),
VAILD_EXCEPTION(10001,"参数格式校验失败");
private int code;
private String msg;
BizCodeEnume(int code, String msg){
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}