文章目录 CommonResult IErrorCode ResultCode
CommonResult
package com. app. tools;
public class CommonResult < T> { private long code; private String message; private T data; protected CommonResult ( ) { } protected CommonResult ( long code, String message, T data) { this . code = code; this . message = message; this . data = data; } public static < T> CommonResult< T> success ( T data) { return new CommonResult < T> ( ResultCode. SUCCESS. getCode ( ) , ResultCode. SUCCESS. getMessage ( ) , data) ; } public static < T> CommonResult< T> success ( T data, String message) { return new CommonResult < T> ( ResultCode. SUCCESS. getCode ( ) , message, data) ; } public static < T> CommonResult< T> failed ( IErrorCode errorCode) { return new CommonResult < T> ( errorCode. getCode ( ) , errorCode. getMessage ( ) , null) ; } public static < T> CommonResult< T> failed ( IErrorCode errorCode, String message) { return new CommonResult < T> ( errorCode. getCode ( ) , message, null) ; } public static < T> CommonResult< T> failed ( String message) { return new CommonResult < T> ( ResultCode. FAILED. getCode ( ) , message, null) ; } public static < T> CommonResult< T> failed ( ) { return failed ( ResultCode. FAILED) ; } public static < T> CommonResult< T> validateFailed ( ) { return failed ( ResultCode. VALIDATE_FAILED) ; } public static < T> CommonResult< T> validateFailed ( String message) { return new CommonResult < T> ( ResultCode. VALIDATE_FAILED. getCode ( ) , message, null) ; } public static < T> CommonResult< T> unauthorized ( T data) { return new CommonResult < T> ( ResultCode. UNAUTHORIZED. getCode ( ) , ResultCode. UNAUTHORIZED. getMessage ( ) , data) ; } public static < T> CommonResult< T> forbidden ( T data) { return new CommonResult < T> ( ResultCode. FORBIDDEN. getCode ( ) , ResultCode. FORBIDDEN. getMessage ( ) , data) ; } public long getCode ( ) { return code; } public void setCode ( long code) { this . code = code; } public String getMessage ( ) { return message; } public void setMessage ( String message) { this . message = message; } public T getData ( ) { return data; } public void setData ( T data) { this . data = data; }
}
IErrorCode
package com. app. tools;
public interface IErrorCode { long getCode ( ) ; String getMessage ( ) ;
}
ResultCode
package com. app. tools; public enum ResultCode implements IErrorCode { SUCCESS ( 200 , "操作成功" ) , FAILED ( 500 , "操作失败" ) , VALIDATE_FAILED ( 404 , "参数检验失败" ) , UNAUTHORIZED ( 401 , "暂未登录或token已经过期" ) , FORBIDDEN ( 403 , "没有相关权限" ) ; private long code; private String message; private ResultCode ( long code, String message) { this . code = code; this . message = message; } public long getCode ( ) { return code; } public String getMessage ( ) { return message; }
}