统一异常处理并错误信息入库
- 1,我们先建一个统一返回给前端的类Result 包含 success code message entity
import com.cmp.business.common.enums.ResultCodeEnum;
/**
* 返回对象
*
* @param <T>
* @author yusheng
* @Description TODO
*/
public class Result<T> {
/**
* 0失败;1成功
*/
private String success = "1";
/**
* 响应码
*/
private String code;
/**
* 返回信息
*/
private String message = "";
/**
* 返回实体
*/
private T entity;
public Result(String success, String message) {
this.success = success;
this.message = message;
}
public Result(String success, String message, T entity) {
this.success = success;
this.message = message;
this.entity = entity;
}
public Result(String success, String code, String message, T entity) {
this.success = success;
this.code = code;
this.message = message;
this.entity = entity;
}
public static <U> Result build(String code, String msg, U data) {
return new Result(code, msg, data);
}
public static <U> Result success() {
return build("1", "success", "");
}
public static <U> Result<U> success(final U data) {
return new Result<U>("1", ResultCodeEnum.SUCCESS.getCode(), "", data);
}
public static Result failure(final String message, final Throwable ex) {
return new Result("0", ResultCodeEnum.FAILURE.getCode(), message, "");
}
public static Resul