在分布式、微服务盛行的今天,绝大部分项目都采用的微服务框架,前后端分离方式。前端和后端进行交互,
前端按照约定请求URL路径,并传入相关参数,后端服务器接收请求,进行业务处理,返回数据给前端。
{
# 是否响应成功
success: true,
# 响应状态码
code: 200,
# 响应数据
data: Object
# 返回错误信息
message: "",
}
code区间 | 类型 | 含义 |
---|---|---|
1** | 100-199 | 信息 服务器接收到请求,需要请求者继续执行操作 |
2** | 200-299 | 成功 请求被成功接收并处理 |
3** | 300-399 | 重定向 需要进一步的操作以完成请求 |
4** | 400-499 | 客户端错误 请求包含语法错误或无法完成请求 |
5** | 500-599 | 服务器错误 服务器在处理的时候发生错误 |
常见的HTTP状态码:
200 - 请求成功;
301 - 资源(网页等)被永久转移到其它URL;
404 - 请求的资源(网页等)不存在;
500 - 内部服务器错误。
@Getter
public enum ResultCodeEnum {
SUCCESS(true, 200,"成功"),
ERROR(false, 500,"失败"),
UNKNOWN_REASON(false, 20001, "未知错误"),
BAD_SQL_GRAMMAR(false, 21001, "sql语法错误"),
JSON_PARSE_ERROR(false, 21002, "json解析异常"),
PARAM_ERROR(false, 21003, "参数不正确"),
FILE_UPLOAD_ERROR(false, 21004, "文件上传错误"),
EXCEL_DATA_IMPORT_ERROR(false, 21005, "Excel数据导入错误");
private Boolean type;
private Integer code;
private String message;
private ResultCodeEnum(Boolean type, Integer code, String message) {
this.type = type;
this.code = code;
this.message = message;
}
}
@Data
@ApiModel(value = "全局统一返回结果")
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "是否成功")
private Boolean state = true;
@ApiModelProperty(value = "返回数据")
private T data;
@ApiModelProperty(value = "返回消息")
private String msg;
@ApiModelProperty(value = "返回的数据总数")
private long count;
@ApiModelProperty(value = "返回码")
private long code;
public Result() {
}
Result(long code, boolean success, String message, long count, T data) {
this.code = code;
this.state = success;
this.msg = message;
this.data = data;
this.count = count;
}
public static <T> Result<T> ok(T data) {
try {
Result<T> result = new Result<T>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getType(), ResultCodeEnum.SUCCESS.getMessage(), 1, data);
return result;
} catch (Exception ex) {
return error(ex);
}
}
public static <T> Result<T> error(Exception ex) {
String format = "Message: %s, StackTrace: %s, Suppressed: %s, Cause: %s, Class: %s ";
String msg = String.format(format, ex.getMessage(), ex.getStackTrace(), ex.getSuppressed(),
ex.getCause(), ex.getClass());
return error(msg);
}
public static <T> Result<T> error(String message) {
Result<T> result = new Result<T>( ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getType(), message, 0, null);
result.count = 0;
return result;
}
}
@Data
public class PagingOptions implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("当前页数")
private Integer pageIndex;
@ApiModelProperty("每页条数")
private Integer pageSize;
@ApiModelProperty("查询条件")
private List<PagingSort> sorts;
@ApiModelProperty("排序条件")
private List<PagingFilter> filters;
}
@Data
public class PagingFilter implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("数据库字段")
private String field;
@ApiModelProperty("条件")
private String op;
@ApiModelProperty("值")
private String value;
}
@Data
public class PagingSort implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("数据库字段")
private String field;
@ApiModelProperty("条件")
private String sort;
}
@Data
public class PageQuery<T> {
private static final long serialVersionUID = 1L;
/**
* 分页的条件
*/
private Page<T> page;
/**
* 当前页数
*/
private int currPage = 1;
/**
* 每页条数
*/
private int limit = 10;
private QueryWrapper<T> queryWrapper;
private BaseMapper baseMapper;
public PageQuery(PagingOptions pagingOptions, BaseMapper baseMapper) {
this.baseMapper = baseMapper;
// 分页的参数
this.page = new Page<>(pagingOptions.getPageIndex(), pagingOptions.getPageSize());
//将每个类的mapper进行赋值
this.queryWrapper = new QueryWrapper<T>();
//组装wrapper的条件
if (pagingOptions.getFilters() != null) {
for (PagingFilter filter : pagingOptions.getFilters()) {
if (StringUtils.isEmpty(filter.getField()) || StringUtils.isEmpty(filter.getOp())) {
continue;
}
String strOp = filter.getOp().trim();
String field = filter.getField().trim();
String term = filter.getValue();
switch (strOp) {
case "eq":
this.queryWrapper.eq(field, term);
break;
case "ne":
this.queryWrapper.ne(field, term);
break;
case "lt":
this.queryWrapper.lt(field, term);
break;
case "le":
this.queryWrapper.le(field, term);
break;
case "gt":
this.queryWrapper.gt(field, term);
break;
case "ge":
this.queryWrapper.ge(field, term);
break;
case "cn":
this.queryWrapper.like(field, term);
break;
case "nu":
this.queryWrapper.isNull(field);
break;
case "nn":
this.queryWrapper.isNotNull(field);
break;
case "in":
List<String> result = Arrays.asList(term.split(","));
this.queryWrapper.in(field, result);
break;
}
}
}
if (pagingOptions.getSorts() != null) {
for (PagingSort sort : pagingOptions.getSorts()) {
if (StringUtils.isEmpty(sort.getField()) || StringUtils.isEmpty(sort.getSort())) {
continue;
}
String strOp = sort.getSort().trim();
switch (strOp) {
case "asc":
this.queryWrapper.orderByAsc(sort.getField());
break;
case "desc":
this.queryWrapper.orderByDesc(sort.getField());
break;
}
}
}
}
}
@Data
@ApiModel(value = "全局统一分页返回结果")
public class PagingResult<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "是否成功")
private Boolean state = true;
@ApiModelProperty(value = "返回数据")
private List<T> data;
@ApiModelProperty(value = "返回消息")
private String msg;
@ApiModelProperty(value = "返回的数据总数")
private long count;
@ApiModelProperty(value = "返回码")
private long code;
PagingResult(long code, boolean success, String message, long count, List<T> data) {
this.setCode(code);
this.setState(success);
this.setMsg(message);
this.setData(data);
this.setCount(count);
}
public static <T> PagingResult<T> ok(PageQuery<T> pageQuery, BaseMapper<T> mapper) {
try {
IPage<T> page = mapper.selectPage(pageQuery.getPage(), pageQuery.getQueryWrapper());
PagingResult<T> result = new PagingResult<T>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getType(), ResultCodeEnum.SUCCESS.getMessage(),
page.getTotal(), page.getRecords());
return result;
} catch (Exception ex) {
return error(ex);
}
}
public static <T> PagingResult<T> error(Exception ex) {
String format = "Message: %s, StackTrace: %s, Suppressed: %s, Cause: %s, Class: %s";
String msg = String.format(format, ex.getMessage(), ex.getStackTrace(), ex.getSuppressed(),
ex.getCause(), ex.getClass());
return error(msg);
}
public static <T> PagingResult<T> error(String message) {
PagingResult<T> result = new PagingResult<T>(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getType(), message, 0, null);
return result;
}
}