栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot返回值实体的封装

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot返回值实体的封装


@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "响应信息主体")
public class R implements Serializable {
	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	@ApiModelProperty(value = "返回标记:成功标记=0,失败标记=1")
	private Integer code;

	@Getter
	@Setter
	@ApiModelProperty(value = "返回信息")
	private String msg;

	@Getter
	@Setter
	@ApiModelProperty(value = "数据")
	private T data;

	@Getter
	@Setter
	@ApiModelProperty(value = "数据")
	private Map totals;

	public static  R ok() {
		return restResult(null, CommonConstants.SUCCESS, "SUCCESS");
	}

	public static  R ok(T data) {
		return restResult(data, CommonConstants.SUCCESS, "SUCCESS");
	}

	public static  R ok(T data, String msg) {
		return restResult(data, CommonConstants.SUCCESS, msg);
	}

	public static  R failed() {
		return restResult(null, CommonConstants.FAIL, null);
	}

	public static  R failed(String msg) {
		return restResult(null, CommonConstants.FAIL, msg);
	}

	public static  R failed(Integer code, String msg) {
		return restResult(null, code, msg);
	}

	public static  R failed(T data) {
		return restResult(data, CommonConstants.FAIL, null);
	}

	public static  R failed(LeaseException e) {
		return restResult(null, e.getCode(), e.getMessage());
	}

	public static  R failed(ErrorCode errorCode) {
		return restResult(null, errorCode.getCode(), errorCode.getMessage());
	}

	public static  R failed(T data, Integer code, String msg) {
		return restResult(data, code, msg);
	}

	public static  R failed(T data, ErrorCode errorCode) {
		return restResult(data, errorCode.getCode(), errorCode.getMessage());
	}

	public static  R failed(ErrorCode errorCode, String msg) {
		return restResult(null, errorCode.getCode(), msg);
	}

	public static  R failed(T data, String msg) {
		return restResult(data, CommonConstants.FAIL, msg);
	}

	private static  R restResult(T data, Integer code, String msg) {
		R apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

	public Boolean getSuccess() {
		return ResultEnums.OK.getCode().equals(code);
	}
}

ErrorCode:

public interface ErrorCode {

    Integer getCode();

    String getMessage();

    int INTERNAL_SERVER_ERROR = 500;
    int UNAUTHORIZED = 401;
    int FORBIDDEN = 403;

    int NOT_NULL = 10001;
    
}
 ErrorCodeEnum:


@AllArgsConstructor
@Getter
public enum ErrorCodeEnum implements ErrorCode {

    OK(200, ""),


    QUERY_ORDER_PHONE_IS_NOT_NULL(20000, "查询订单的手机号不能为空"),
    ;


    
    private final Integer code;

    
    private final String message;

}

ResultEnums:
@AllArgsConstructor
@Getter
public enum ResultEnums {

	// 成功
	OK(0, "SUCCESS"),
	// 失败
	ERROR(1, "ERROR"),


	;


	
	private final Integer code;

	
	private final String name;
CommonConstants:

public interface CommonConstants {
	
	
	Integer SUCCESS = 0;
	
	Integer FAIL = 1;


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306628.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号