通用返回类型定义

定义业务异常类:

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class BusinessException extends RuntimeException{
    private String errorCode;
    private String errorMsg;
}

错误枚举类:

public enum UnifiedResponseEnums {
    BAD_PARAM("1002","参数有错"),
    NOT_FOUND("1003","资源不存在"),
    NO_PERMISSION("1004","权限不足"),
    BAD_INPUT_PARAM("1005","入参有问题"),
    BAD_EMAIL_PARAM("1006","email入参有问题"),
    BAD_ID_PARAM("1007","id入参有问题"),
    BAD_IP("1008","非法IP"),
    INVALLD_TOKEN("1009","无效令牌"),
    TOO_MANY_PARAMS("1010","参数过载");
    private String errorCode;
    private String errorMsg;

    UnifiedResponseEnums(String errorCode, String errorMsg) {
        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;
    }
}

Controller 中使用:

UnifiedResponseEnums enum1 = UnifiedResponseEnums.valueOf(UnifiedResponseEnums.class,"BAD_PARAM");
throw new BusinessException(enum1.getErrorCode(), enum1.getErrorMsg());

设置通用返回结果:

public class JsonResultObject {
    private String Code;
    private String errorCode;
    private String message;
    private String errorMessage;
    private T data;

    public JsonResultObject(String code, String message, T data) {
        Code = code;
        this.message = message;
        this.data = data;
    }

    public JsonResultObject(String code, String errorCode, String message, String errorMessage, T data) {
        Code = code;
        this.errorCode = errorCode;
        this.message = message;
        this.errorMessage = errorMessage;
        this.data = data;
    }

    public JsonResultObject(String errorCode, String errorMessage) {
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public String getCode() {
        return Code;
    }

    public void setCode(String code) {
        Code = code;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getData() {
        return data;
    }

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

错误返回定义:


public class ErrorResult {
    private String errorMessage;
    private String errorCode;
    private T data;

    public ErrorResult(String errorCode,String errorMessage, T data) {
        this.errorMessage = errorMessage;
        this.errorCode = errorCode;
        this.data = data;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public T getData() {
        return data;
    }

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

 

你可能感兴趣的:(spring-boot,java,开发语言,spring,boot)