1.全局捕获异常:进行全局捕获异常可以去捕获一些运行时候的错误,将这些错误信息处理掉,而不去返回给用户。
2.代码如下:
1.自定义已给枚举类,做统一管理
/**
* 功能描述:自定义一个枚举类,进行错误码的统一管理
*/
public enum ResultEnum {
SUCCESS(0, "成功"),
FAILURE(1, "失败"),
/* 参数错误:10001-19999 */
PARAM_IS_INVALID(10001, "参数无效"),
PARAM_IS_BLANK(10002, "参数为空"),
PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
PARAM_NOT_COMPLETE(10004, "参数缺失"),
/* 用户错误:20001-29999*/
USER_NOT_LOGGED_IN(20001, "用户未登录"),
USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),
USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
USER_NOT_EXIST(20004, "用户不存在"),
USER_HAS_EXISTED(20005, "用户已存在"),
/* 业务错误:30001-39999 */
SPECIFIED_QUESTIONED_USER_NOT_EXIST(30001, "某业务出现问题"),
/* 系统错误:40001-49999 */
SYSTEM_INNER_ERROR(40001, "系统繁忙,请稍后重试"),
/* 数据错误:50001-599999 */
RESULE_DATA_NONE(50001, "数据未找到"),
DATA_IS_WRONG(50002, "数据有误"),
DATA_ALREADY_EXISTED(50003, "数据已存在"),
/* 接口错误:60001-69999 */
INTERFACE_INNER_INVOKE_ERROR(60001, "内部系统接口调用异常"),
INTERFACE_OUTTER_INVOKE_ERROR(60002, "外部系统接口调用异常"),
INTERFACE_FORBID_VISIT(60003, "该接口禁止访问"),
INTERFACE_ADDRESS_INVALID(60004, "接口地址无效"),
INTERFACE_REQUEST_TIMEOUT(60005, "接口请求超时"),
INTERFACE_EXCEED_LOAD(60006, "接口负载过高"),
/* 权限错误:70001-79999 */
PERMISSION_NO_ACCESS(70001, "无访问权限");
private Integer code;
private String msg;
ResultEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer code() {
return this.code;
}
public String message() {
return this.msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
2.所需调取返回的工具类(也可用泛型进行封装修改)
/**
* http请求返回的最外层对象
*/
public class Result {
/**
* 错误码.
*/
private Integer code;
/**
* 提示信息.
*/
private String msg;
/**
* 具体的内容.
*/
private Object data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public void setResultCode(ResultEnum code) {
this.code = code.code();
this.msg = code.message();
}
//成功 不返回数据直接返回成功信息
public static Result success() {
Result result = new Result();
result.setResultCode(ResultEnum.SUCCESS);
return result;
}
//成功 并且加上返回数据
public static Result success(Object data) {
Result result = new Result();
result.setResultCode(ResultEnum.SUCCESS);
result.setData(data);
return result;
}
//成功 自定义成功返回状态 加上数据
public static Result success(ResultEnum resultEnum, Object data) {
Result result = new Result();
result.setResultCode(resultEnum);
result.setData(data);
return result;
}
// 单返回失败的状态码
public static Result failure(ResultEnum resultEnum) {
Result result = new Result();
result.setResultCode(resultEnum);
return result;
}
// 返回失败的状态码 及 数据
public static Result failure(ResultEnum resultEnum, Object data) {
Result result = new Result();
result.setResultCode(resultEnum);
result.setData(data);
return result;
}
//进行自定义异常消息 覆盖原始定义消息
public static Result error(Integer code, String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}
3.自定义全局异常处理
/**
* 功能描述:自定义全局异常处理
* 之所以继承RuntimeException因为其在Spring中会进行事务回滚 而其父类不会
*/
public class CustomizeException extends RuntimeException {
private Integer code;
public CustomizeException(ResultEnum resultEnum) {
super(resultEnum.getMsg());
this.code = resultEnum.getCode();
}
//新增msg 进行自定义异常消息
public CustomizeException(ResultEnum resultEnum, String msg) {
super(msg);
this.code = resultEnum.getCode();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
4.全局异常捕获:
import com.itcast.exception.CustomizeException;
import com.itcast.util.Result;
import lombok.extern.slf4j.Slf4j;
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;
/**
* 功能描述:捕获异常类
*/
@ControllerAdvice
@Slf4j
public class ExceptionHandle {
//拦截的错误
@ExceptionHandler(value = Exception.class)
//一些异常是因为请求逻辑导致,而非服务器本身内部处理异常,这时服务器端是接受了请求,而在返回时发生异常,这时服务器接受请求
//的状态是成功的,此时再处理请求逻辑异常,将会进入这个方法处理。需要ResponseStatus注解
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Result handle(Exception e) {
//如果异常为自定义异常就走自定义异常
if (e instanceof CustomizeException) {
CustomizeException girlException = (CustomizeException) e;
log.error("【自定义异常】{}", girlException.getMessage());
return Result.error(girlException.getCode(), girlException.getMessage());
} else {
log.error("【系统异常】{}", e);
return Result.error(-1, "未知错误");
}
}
}
5.测试
@ApiOperation(value = "全局捕获异常测试", notes = "全局捕获异常测试")
@PutMapping("code")
@ResponseBody
public Result ApiCode(@RequestBody Student student) {
if (student.getAge() == 0) {
throw new CustomizeException(ResultEnum.FAILURE, "测试");
}
return Result.success(student);
}