通用信息返回对象

Posted by Kaka Blog on January 2, 2020

通用信息返回对象

public class CommonReturnType<T> {
    private String status;
    private T data;

    public static <T> CommonReturnType success(T data) {
        return create("success", data);
    }

    public static CommonReturnType fail(CommonError data) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("errCode", data.getErrCode());
        map.put("errMsg", data.getErrMsg());
        return create("fail", map);
    }

    private static <T> CommonReturnType create(String status, T data) {
        CommonReturnType res = new CommonReturnType();
        res.status = status;
        res.data = data;
        return res;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

错误信息返回对象

1、定义接口

public interface CommonError {
    int getErrCode();
    String getErrMsg();
    /**
     * 覆盖错误信息
     **/
    CommonError setErrMsg(String errMsg);
}

2、定义错误信息枚举类

public enum EmBusinessError implements CommonError {
    PARAMETER_VALID_ERROR(999999, "参数不合法"),
    UNKNOWN_ERROR(666666, "未知错误"),
    USER_NOT_EXIST(100000, "用户不存在")
    ;
    private int errCode;
    private String errMsg;

    private EmBusinessError(int errCode, String errMsg) {
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    @Override
    public int getErrCode() {
        return this.errCode;
    }

    @Override
    public String getErrMsg() {
        return this.errMsg;
    }

    @Override
    public CommonError setErrMsg(String errMsg) {
        this.errMsg = errMsg;
        return this;
    }
}

3、定义异常,装饰器模式

public class BusinessException extends Exception implements CommonError {
    private CommonError commonError;

    public BusinessException(CommonError commonError) {
        super();
        this.commonError = commonError;
    }

    // 自定义errMsg方式
    public BusinessException(CommonError commonError, String errMsg) {
        super();
        this.commonError = commonError;
        this.commonError.setErrMsg(errMsg);
    }

    @Override
    public int getErrCode() {
        return this.commonError.getErrCode();
    }

    @Override
    public String getErrMsg() {
        return this.commonError.getErrMsg();
    }

    @Override
    public CommonError setErrMsg(String errMsg) {
        this.commonError.setErrMsg(errMsg);
        return this;
    }
}

4、异常处理,定义ExceptionHandler,解决未被controller层吸收的Exception。

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
public Object handlerException(HttpServletRequest request, Exception ex) {
    CommonError commonError;
    if (ex instanceof BusinessException) {
        BusinessException be = (BusinessException) ex;
        commonError = be;
    }
    else {
        commonError = EmBusinessError.UNKNOWN_ERROR;
    }
    return CommonReturnType.fail(commonError);
}

5、测试类

@GetMapping("/get")
public CommonReturnType get(String uid) throws BusinessException {
    try {
        int id = Integer.valueOf(uid);
    }
    catch (NumberFormatException ex) {
        throw new BusinessException(EmBusinessError.PARAMETER_VALID_ERROR, "uid输入错误");
    }
    if ("123".equals(uid)) {
        return CommonReturnType.success(uid);
    }
    throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
}

参考资料