提交 d65b4329 编写于 作者: cxt104926's avatar cxt104926

1

上级 f89180ca
package com.stu.stusystem.common;
import com.stu.stusystem.exception.CommonEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
......@@ -25,7 +26,7 @@ public class ApiResult<T> implements Serializable {
private Boolean success;
@ApiModelProperty(value = "状态码", required = true)
private int code;
private Integer code;
@ApiModelProperty(value = "消息", required = true)
private String message;
......@@ -33,13 +34,13 @@ public class ApiResult<T> implements Serializable {
@ApiModelProperty(value = "返回数据")
private T data;
public ApiResult(Boolean success, int code, String message) {
public ApiResult(Boolean success, Integer code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
public ApiResult(int code, String message) {
public ApiResult(Integer code, String message) {
this.success = false;
this.code = code;
this.message = message;
......@@ -104,7 +105,14 @@ public class ApiResult<T> implements Serializable {
/**
* 错误
*/
public static <T> ApiResult<T> error(int code, String message) {
public static <T> ApiResult<T> error(CommonEnum e) {
return new ApiResult<T>(false, 500, e.getResultMsg());
}
/**
* 错误
*/
public static <T> ApiResult<T> error(Integer code, String message) {
return new ApiResult<T>(false, code, message);
}
}
package com.stu.stusystem.exception;
/**
* 异常信息接口
*/
public interface BaseErrorInfoInterface {
/**
* 错误码
*/
Integer getResultCode();
/**
* 错误描述
*/
String getResultMsg();
}
\ No newline at end of file
package com.stu.stusystem.exception;
/**
* @author: cxt
* @time: 2021/3/10
* 自定义异常类
*/
public class BizException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
protected String errorCode;
/**
* 错误信息
*/
protected String errorMsg;
public BizException() {
super();
}
public BizException(BaseErrorInfoInterface errorInfoInterface) {
super(errorInfoInterface.getResultCode());
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
}
public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
super(errorInfoInterface.getResultCode(), cause);
this.errorCode = errorInfoInterface.getResultCode();
this.errorMsg = errorInfoInterface.getResultMsg();
}
public BizException(String errorMsg) {
super(errorMsg);
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public BizException(String errorCode, String errorMsg, Throwable cause) {
super(errorCode, cause);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getMessage() {
return errorMsg;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
\ No newline at end of file
package com.stu.stusystem.exception;
/**
* 异常枚举类
*/
public enum CommonEnum implements BaseErrorInfoInterface {
// 数据操作错误定义
SUCCESS("200", "成功!"),
BODY_NOT_MATCH("400", "请求的数据格式不符!"),
SIGNATURE_NOT_MATCH("401", "请求的数字签名不匹配!"),
NOT_FOUND("404", "未找到该资源!"),
INTERNAL_SERVER_ERROR("500", "服务器内部错误!"),
SERVER_BUSY("503", "服务器正忙,请稍后再试!");
/**
* 错误码
*/
private String resultCode;
/**
* 错误描述
*/
private String resultMsg;
CommonEnum(String resultCode, String resultMsg) {
this.resultCode = resultCode;
this.resultMsg = resultMsg;
}
@Override
public String getResultCode() {
return resultCode;
}
@Override
public String getResultMsg() {
return resultMsg;
}
}
package com.stu.stusystem.exception;
import com.stu.stusystem.common.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
* @author: cxt
* @time: 2021/3/10
* 全局异常处理
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 处理自定义的业务异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value = BizException.class)
@ResponseBody
public ApiResult bizExceptionHandler(HttpServletRequest req, BizException e){
log.error("发生业务异常!原因是:{}",e.getErrorMsg());
return ApiResult.error(Integer.parseInt(e.getErrorCode()),e.getErrorMsg());
}
/**
* 处理空指针的异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =NullPointerException.class)
@ResponseBody
public ApiResult exceptionHandler(HttpServletRequest req, NullPointerException e){
log.error("发生空指针异常!原因是:",e);
return ApiResult.error(CommonEnum.BODY_NOT_MATCH);
}
/**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler(value =Exception.class)
@ResponseBody
public ApiResult exceptionHandler(HttpServletRequest req, Exception e){
log.error("未知异常!原因是:",e);
return ApiResult.error(CommonEnum.INTERNAL_SERVER_ERROR);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册