目录结构:
Result类:
package com.xxxx.common.result;import lombok.Data;@Data
public class Result<T> {//状态码private Integer code;//信息private String message;//数据private T data;//构造私有化private Result() { }//设置数据,返回对象的方法public static<T> Result<T> build(T data,ResultCodeEnum resultCodeEnum) {//创建Resullt对象,设置值,返回对象Result<T> result = new Result<>();//判断返回结果中是否需要数据if(data != null) {//设置数据到result对象result.setData(data);}//设置其他值result.setCode(resultCodeEnum.getCode());result.setMessage(resultCodeEnum.getMessage());//返回设置值之后的对象return result;}//成功的方法public static<T> Result<T> ok(T data) {Result<T> result = build(data, ResultCodeEnum.SUCCESS);return result;}//失败的方法public static<T> Result<T> fail(T data) {return build(data,ResultCodeEnum.FAIL);}
}
ResultCodeEnum枚举类:
package com.xxxx.common.result;import lombok.Getter;
/*** 统一返回结果状态信息类**/
@Getter
public enum ResultCodeEnum {SUCCESS(200,"成功"),FAIL(201, "失败"),SERVICE_ERROR(2012, "服务异常"),DATA_ERROR(204, "数据异常"),ILLEGAL_REQUEST(205, "非法请求"),REPEAT_SUBMIT(206, "重复提交"),LOGIN_AUTH(208, "未登陆"),PERMISSION(209, "没有权限"),URL_ENCODE_ERROR( 216, "URL编码失败"),ILLEGAL_CALLBACK_REQUEST_ERROR( 217, "非法回调请求"),FETCH_ACCESSTOKEN_FAILD( 218, "获取accessToken失败"),FETCH_USERINFO_ERROR( 219, "获取用户信息失败"),;private Integer code;private String message;private ResultCodeEnum(Integer code, String message) {this.code = code;this.message = message;}
}
脚踏实地行,海阔天空飞