异常 、返回值、枚举封装
异常
package com.hainei.common.exception; import com.hainei.common.exception.code.BaseResponseCode; import com.hainei.common.exception.code.BusinessResponseCode; /** * Created with IntelliJ IDEA. * User: lzx * Date: 2020/02/13 * Time: 17:16 * Description: 业务异常类 */ public class BusinessException extends RuntimeException{ private final int code; private final String defaultMessage; public BusinessException(int code, String defaultMessage) { super(defaultMessage); this.code = code; this.defaultMessage = defaultMessage; } public BusinessException(BaseResponseCode baseResponseCode){ this(baseResponseCode.getCode(), baseResponseCode.getMsg()); } public BusinessException(SafetyResponseCode safetyResponseCode){ this(safetyResponseCode.getCode(), safetyResponseCode.getMsg()); } public BusinessException(String defaultMessage) { this(400,defaultMessage); } public int getCode() { return code; } public String getDefaultMessage() { return defaultMessage; } }
返回值
package com.hainei.common.utils; import com.hainei.common.exception.code.BaseResponseCode; import com.hainei.common.exception.code.ResponseCodeInterface; import io.swagger.annotations.ApiModelProperty; import lombok.Data; /** * Created with IntelliJ IDEA. * User: lzx * Date: 2020/02/14 * Time: 12:14 * Description: No Description */ @Data public class DataResult <T>{ /** * 请求响应code, 0表示请求成功 其它表示失败 */ @ApiModelProperty(value = "请求响应code,0为成功 其他为失败") private int code; /** * 响应客户端的提示 */ @ApiModelProperty(value = "响应异常码详细信息") private String msg; /** * 响应客户端内容 */ @ApiModelProperty(value = "响应客户端内容") private T data; public DataResult(int code,T data){ this.code = code; this.data = data; this.msg=null; } public DataResult(int code,String msg,T data){ this.code = code; this.msg = msg; this.data = data; } public DataResult(int code, String msg) { this.code = code; this.msg = msg; this.data=null; } public DataResult() { this.code= BaseResponseCode.SUCCESS.getCode(); this.msg=BaseResponseCode.SUCCESS.getMsg(); this.data=null; } public DataResult(T data) { this.data = data; this.code=BaseResponseCode.SUCCESS.getCode(); this.msg=BaseResponseCode.SUCCESS.getMsg(); } public DataResult(ResponseCodeInterface responseCodeInterface) { this.data = null; this.code = responseCodeInterface.getCode(); this.msg = responseCodeInterface.getMsg(); } public DataResult(ResponseCodeInterface responseCodeInterface, T data) { this.data = data; this.code = responseCodeInterface.getCode(); this.msg = responseCodeInterface.getMsg(); } /** * 操作成功 * @param <T> */ public static <T>DataResult success(){ return new <T>DataResult(); } /** * 操作成功,data不为null * @param data * @param <T> */ public static <T>DataResult success(T data){ return new <T>DataResult(data); } /** * 自定义 返回操作 data 可控 * @param code * @param msg * @param data * @param <T> */ public static <T>DataResult getResult(int code,String msg,T data){ return new <T>DataResult(code,msg,data); } /** * 自定义返回 data为null * @param code * @param msg * @param <T> */ public static <T>DataResult getResult(int code,String msg){ return new <T>DataResult(code,msg); } /** * 自定义返回 入参一般是异常code枚举 data为空 * @param responseCode * @param <T> */ public static <T>DataResult getResult(BaseResponseCode responseCode){ return new <T>DataResult(responseCode); } /** * 自定义返回 入参一般是异常code枚举 data 可控 * @param responseCode * @param data * @param <T> */ public static <T>DataResult getResult(BaseResponseCode responseCode, T data){ return new <T>DataResult(responseCode,data); } }
枚举
package com.hainei.common.exception.code; /** * Created with IntelliJ IDEA. * User: lzx * Date: 2020/03/12 * Time: 16:04 * Description: code=400xxxxx:系统主动抛出的业务异常 */ public enum BusinessResponseCode implements ResponseCodeInterface{ /** * 4001xxxx 消息中心-业务异常 */ MSG_CONFIG_MISS_ERROR(40010001,"请先配置短信基础配置"), MSG_INVALID_ACCESS_KEY_ERROR(40010002,"不存在此accesskeyId"), MSG_SIGNATURE_NOT_MATCH_EROOR(40010003, "密码不匹配"), MSG_TEMPLATE_NOT_EXIST_EROOR(40010004,"该消息模板已不存在"), MSG_TEMPALTE_NOT_FINISHED_ERROR(40010005,"该消息模板尚未通过,无法使用进行发送短信"), /** * 4002XXX 文件导出配置 */ TEMPLATE_IMPORT_FORMAT_ERROR(4002001,"文件格式不正确"), TEMPLATE_IMPORT_UNUSAUAL_ERROR(4002002,"导入异常"), TEMPLATE_EXPORT_EXIST_ERROR(4002003,"模版不存在"), TEMPLATE_EXPORT_MORE_ERROR(4002004,"检索数据超过一条,无法导出"), TEMPLATE_EXPORT_NOTHING_ERROR(4002005,"没有检索到数据,无法导出"), TEMPLATE_EXPORT_UNUSAUAL_ERROR(4002002,"导出异常"), TEMPLATE_TYPE_DELETE_ERROR(4002002,"分类下有子分类,无法删除"), /** * activiti异常 */ WORKFLOW_DELETE_ERROR(4003001,"流程尚未结束,不能删除"), WORKFLOW_MODEL_HAS_SAME_KEY_ERROR(4003002,"已经存在相同标识的流程模型"), WORKFLOW_MODEL_DEPLOY_ERROR(4003002,"部署失败"), WORKFLOW_MODEL_NOT_DEPLOYED_ERROR(4003002,"流程尚未部署,无法查看代码"), WORKFLOW_MODEL_HAS_DEPLOYED_ERROR(4003002,"模型已经部署,无法删除"), WORKFLOW_FLOW_SELECT_ERROR(4003002,"流程条件获取失败,请联系管理员"), WORKFLOW_DEFINITION_SELECT_ERROR(4003002,"该标识下已经存在相同值的描述了"), WORKFLOW_PROCESS_INST_SET_ASSIGNEE_ERROR(4003003,"任务转办失败"), WORKFLOW_PROCESS_INST_GOTO_ERROR(4003003,"任务跳转失败"), WORKFLOW_BUSINESS_REGIST_SAME_NAME_ERROR(4003004, "已经存在相同名称的业务组件名称"), /** * ldar */ LDAR_DATA_HAS_CHILD_ERROR(4008001,"当前数据被引用,无法删除"), LDAR_HAS_SAME_DATA_ERROR(4008001,"名称已存在,请重新添加"), LDAR_LET_POINT_HAS_SAME_DATA_ERROR(4008002,"该点位当年数据已录入"), /** * 企业接入大屏 */ DATA_NOTFOUND_ERROR(4007001,"数据不存在") ; /** * 响应码 */ private int code; /** * 提示 */ private String msg; BusinessResponseCode(int code, String msg) { this.code = code; this.msg = msg; } @Override public int getCode() { return code; } @Override public String getMsg() { return msg; } }
枚举类接口
package com.hainei.common.exception.code; public interface ResponseCodeInterface { int getCode(); String getMsg(); }
个人学习笔记,记录日常学习,便于查阅及加深,仅为方便个人使用。

浙公网安备 33010602011771号