springCloud 服务提供者应返回的统一的数据格式
package com.zledu.commonentity.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* @Author: husToy.Wang
* @Date: 2019/6/27 8:55
* @Version 1.0
* @date 2019-06-27 08:58
* @author wangzhilei
*/
@Data
public class BaseResult implements Serializable {
private static final String OK = "OK";
private static final String NotOK = "notOk" ;
private static final String SUCCESS = "操作成功";
private String result;
private Object data;
private String success;
private Cursor cursor;
private List<Error> errors;
// TODO 返回无参ok
public static BaseResult ok(){
return createRestult(OK, null, SUCCESS,null,null ) ;
}
// TODO 返回带success 的 ok
public static BaseResult ok(String success){
return createRestult(OK, null, success, null, null);
}
// TODO 返回带 data 的ok
public static BaseResult ok(Object data){
return createRestult(OK, data, SUCCESS, null, null);
}
// 返回带分页的数据
public static BaseResult ok(Object data, Cursor cursor) {
return createRestult(OK, data, SUCCESS, cursor, null);
}
// TODO 返回错误数据
public static BaseResult notOk(List<BaseResult.Error> errors){
return createRestult(NotOK, null, null, null, errors);
}
// TODO 构建错误与正确方法
public static BaseResult createRestult(String result, Object data,String success, Cursor cursor, List<BaseResult.Error> errors){
BaseResult baseResult = new BaseResult();
baseResult.setResult(result);
baseResult.setData(data);
baseResult.setSuccess(success);
baseResult.setCursor(cursor);
baseResult.setErrors(errors);
return baseResult;
}
@Data
@AllArgsConstructor
public static class Cursor {
private int total;
private int offset;
private int limit;
}
@Data
@AllArgsConstructor
public static class Error{
private String field;
private String message;
}
}
技术改变一切