异常统一捕获和处理,还有数据统一的封装。这个是在项目里面经常会用到。所以我写了一个。以供自己方便使用。当一个笔记的编写。
1、BaseExcpetion,是异常基本运行时异常。
package com.test.cn;
/**
* 封装运行时异常
*
* @author BlockChain
*
*/
public class BaseException extends RuntimeException {
private static final long serialVersionUID = -6693895810786471043L;
/**
* 错误码,相关定义规范请参考《异常定义规范》
*/
private String errorcode;
public BaseException(String errorcode,String message,Throwable e) {
super(message,e);
this.errorcode = errorcode;
}
public String getErrorcode() {
return errorcode;
}
}
2、BusinessException是业务性异常。
package com.test.cn;
/**
* 封装业务异常,一般在正常情况下产生, 开发人员需要根据情况分别处理
*/
public class BusinessException extends Exception {
private static final long serialVersionUID = -1986655358889488649L;
/**
* 错误码,相关定义规范请参考《异常定义规范》
*/
private String errorcode;
public BusinessException(String errorcode,String message,Throwable e) {
super(message,e);
this.errorcode = errorcode;
}
public String getErrorcode() {
return errorcode;
}
}
3、ResponseBaseObject, 是一个定义的基本类。
package com.test.cn;
import java.io.Serializable;
/**
* 对外返值的基本类
* @author BlockChain
*
* @param <T>
*/
public class ResponseBaseObject<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5727186334451864680L;
private String code;
private Boolean success;
private String msg;
private T data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
4、 ResponseObject,对异常和返回值进行封装。
package com.test.cn;
/**
*
*
* @author BlockChain
*
* @param <T>
*/
public class ResponseObject<T> extends ResponseBaseObject<T> {
/**
*
*/
private static final long serialVersionUID = 5981132252304075190L;
protected static final String SUCCESS_CODE = "0";//成功
//无参构造,专门做框架时候要用到,其实这里可以指定为private 但是有可能有特殊的情况,所以为了方便就该成了公开的权限了
public ResponseObject() {}
//有参数的构造也是。。尽量使用下面的静态方法
public ResponseObject(Boolean issucce,String code,String msg,T resultObj) {
if (issucce) {
this.setCode(code);
}else {
this.setCode(code);
this.setMsg(msg);
}
this.setCode(code);
this.setData(resultObj);
}
//成功
public static <T> ResponseObject<T> success(){
return new ResponseObject<T>(true, SUCCESS_CODE, null, null);
}
/**
* 成功
*
* @param data
* @return
*/
public static <T> ResponseObject<T> success(T resultObj){
return new ResponseObject<T>(true, SUCCESS_CODE, null, resultObj);
}
/**
* 运行时,业务异常
*
* @param e
* @return
*/
public static <T> ResponseObject<T> failBaseRuntimeException(BaseException e){
return new ResponseObject<T>(false, e.getErrorcode(), e.getMessage(), null);
}
/**
* 返回失败,未被定义的业务错误
*
* @param e
* @return
*/
public static <T> ResponseObject<T> failRuntimeException(RuntimeException e){
if (e instanceof BaseException) {
return failBaseRuntimeException((BaseException)e);
}else {
return new ResponseObject<T>(false, "EX101", e.getMessage(), null);
}
}
public static void checkBusinessException(ResponseObject<?> resp)throws BusinessException{
if(!resp.getSuccess()&&!"0".equals(resp.getCode())) {
String errorCode = resp.getCode()+"";
if (errorCode.startsWith("EX")) {
throw new BaseException(errorCode, resp.getMsg(), null);
}else {
throw new BusinessException(errorCode, resp.getMsg(), null);
}
}
}
public static void checkRuntimeException(ResponseObject<?> resp) {
if (!resp.getSuccess()&&!"0".equals(resp.getCode())) {
String errorCode = resp.getCode()+"";
if (errorCode.startsWith("EX")) {
throw new BaseException(errorCode, resp.getMsg(), null);
}else {
BusinessException businessException = new BusinessException(errorCode, resp.getMsg(), null);
throw new BaseException(errorCode, "EX201", businessException);
}
}
}
public static Exception getThrowException(ResponseObject<?> resp) {
if (!resp.getSuccess()&&!"0".equals(resp.getCode())) {
String errorCode = resp.getCode()+"";
if (errorCode.startsWith("EX")) {
return new BaseException(errorCode, resp.getMsg(), null);
}else {
return new BusinessException(errorCode, resp.getMsg(), null);
}
}else {
return null;
}
}
}