Ver código fonte

feat:
1. 增加全局异常处理
2. 增加全局返回参数
3. 对外接口,返回统计格式参数

Jing.Li 4 anos atrás
pai
commit
4bd8fdd7a7

+ 33 - 0
src/main/java/com/yideb/audit/config/GlobalExceptionHandler.java

@@ -0,0 +1,33 @@
+package com.yideb.audit.config;
+
+import com.yideb.audit.ret.RetData;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * <b>Application name:</b> yideb-audit-statistic <br>
+ * <b>Application describing: </b> <br>
+ * <b>Copyright:</b> Copyright &copy; 2020 明医众禾科技(北京)有限责任公司 版权所有。<br>
+ * <b>Company:</b> 明医众禾科技(北京)有限责任公司 <br>
+ * <b>@Date:</b> 2020/8/11 16:47 <br>
+ * <b>@Author:</b> <a href="mailto:lijing@miyzh.com"> 李璟 </a> <br>
+ * <b>@Checker:</b>
+ */
+@Slf4j
+@Component
+@RestControllerAdvice
+public class GlobalExceptionHandler {
+
+	@ExceptionHandler(value = Exception.class)
+	public RetData defaultErrorHandler(HttpServletRequest req, Exception exception) {
+		log.info("=========Exception[begin]==========");
+		log.error("error = {}", exception);
+		log.info("=========Exception[end]==========");
+
+		return RetData.fail("系统操作异常");
+	}
+}

+ 44 - 0
src/main/java/com/yideb/audit/ret/RetCode.java

@@ -0,0 +1,44 @@
+package com.yideb.audit.ret;
+
+import java.io.Serializable;
+
+/**
+ * <b>Application name:</b> yideb-audit-statistic <br>
+ * <b>Application describing: </b> <br>
+ * <b>Copyright:</b> Copyright &copy; 2020 明医众禾科技(北京)有限责任公司 版权所有。<br>
+ * <b>Company:</b> 明医众禾科技(北京)有限责任公司 <br>
+ * <b>@Date:</b> 2020/8/11 16:58 <br>
+ * <b>@Author:</b> <a href="mailto:lijing@miyzh.com"> 李璟 </a> <br>
+ * <b>@Checker:</b>
+ */
+public class RetCode implements Serializable {
+	public static RetCode SUCCESS = new RetCode(0, "成功");
+	public static RetCode FAIL = new RetCode(-1, "失败");
+	public static RetCode UNKNOWN_EXCEPTION = new RetCode(500, "服务器发生内部异常");
+	public static RetCode RESOURCE_UNAUTHORIZED = new RetCode(403, "未授权的服务器资源");
+	public static RetCode RESOURCE_NOT_EXIST = new RetCode(404, "访问的资源不存在");
+	public static RetCode PARAMETER_FORMAT_ERROR = new RetCode(405, "参数格式不符合要求");
+	public static RetCode PARAMETER_REQUIRED = new RetCode(406, "缺少请求参数");
+	public static RetCode TOKEN_EXPIRE = new RetCode(600, "用户会话失效或过期");
+	public static RetCode USER_NOT_EXIST = new RetCode(601, "用户不存在");
+	public static RetCode USER_AND_PASSWORD_ERROR = new RetCode(602, "用户密码错误");
+	public static RetCode VALIDATE_CODE_ERROR = new RetCode(603, "验证码无效");
+	private Integer code;
+	private String msg;
+
+	public RetCode() {
+	}
+
+	public RetCode(Integer code, String msg) {
+		this.code = code;
+		this.msg = msg;
+	}
+
+	public Integer getCode() {
+		return this.code;
+	}
+
+	public String getMsg() {
+		return this.msg;
+	}
+}

+ 121 - 0
src/main/java/com/yideb/audit/ret/RetData.java

@@ -0,0 +1,121 @@
+package com.yideb.audit.ret;
+
+import java.io.Serializable;
+
+/**
+ * <b>Application name:</b> yideb-audit-statistic <br>
+ * <b>Application describing: </b> <br>
+ * <b>Copyright:</b> Copyright &copy; 2020 明医众禾科技(北京)有限责任公司 版权所有。<br>
+ * <b>Company:</b> 明医众禾科技(北京)有限责任公司 <br>
+ * <b>@Date:</b> 2020/8/11 16:59 <br>
+ * <b>@Author:</b> <a href="mailto:lijing@miyzh.com"> 李璟 </a> <br>
+ * <b>@Checker:</b>
+ */
+public class RetData<T> implements Serializable {
+	private Integer code;
+	private String msg;
+	private T data;
+
+	public RetData() {
+		this.code = RetCode.SUCCESS.getCode();
+		this.msg = RetCode.SUCCESS.getMsg();
+	}
+
+	public RetData(RetCode retCode) {
+		this.code = retCode.getCode();
+		this.msg = retCode.getMsg();
+	}
+
+	public Integer getCode() {
+		return this.code;
+	}
+
+	public void setCode(Integer code) {
+		this.code = code;
+	}
+
+	public void setCode(RetCode retCode) {
+		this.code = retCode.getCode();
+		this.msg = retCode.getMsg();
+	}
+
+	public void setCode(RetCode retCode, String msg) {
+		this.code = retCode.getCode();
+		this.msg = msg;
+	}
+
+	public String getMsg() {
+		return this.msg;
+	}
+
+	public void setMsg(String msg) {
+		this.msg = msg;
+	}
+
+	public T getData() {
+		return this.data;
+	}
+
+	public void setData(T data) {
+		this.data = data;
+	}
+
+	public static RetData success() {
+		return ret(RetCode.SUCCESS);
+	}
+
+	public static RetData success(Object data) {
+		return ret(RetCode.SUCCESS, data);
+	}
+
+	public static RetData success(String msg) {
+		return ret(RetCode.SUCCESS, msg);
+	}
+
+	public static RetData success(String msg, Object data) {
+		return ret(RetCode.SUCCESS, msg, data);
+	}
+
+	public static RetData fail() {
+		return ret(RetCode.FAIL);
+	}
+
+	public static RetData fail(String msg) {
+		return ret(RetCode.FAIL, msg);
+	}
+
+	public static RetData fail(Object data) {
+		return ret(RetCode.FAIL, data);
+	}
+
+	public static RetData ret(RetCode retCode, String msg, Object data) {
+		RetData retData = new RetData(retCode);
+		retData.setData(data);
+		retData.setMsg(msg != null ? msg : retData.getMsg());
+		return retData;
+	}
+
+	public static RetData ret(RetCode retCode, String msg) {
+		return ret((RetCode)retCode, msg, (Object)null);
+	}
+
+	public static RetData ret(RetCode retCode, Object data) {
+		return ret((RetCode)retCode, (String)null, data);
+	}
+
+	public static RetData ret(RetCode retCode) {
+		return ret((RetCode)retCode, (String)null, (Object)null);
+	}
+
+	public static RetData ret(Integer code, String msg, Object data) {
+		RetData retData = new RetData();
+		retData.setCode(code);
+		retData.setMsg(msg);
+		retData.setData(data);
+		return retData;
+	}
+
+	public static RetData ret(Integer code, String msg) {
+		return ret((Integer)code, msg, (Object)null);
+	}
+}