介绍
在项目开发中,很多时候会写一个restful接口,对于此类接口为了前端方便处理,会额外增加一些标识状态的字段,比如seccess、status等
二,实现过程和示例代码
- 新建一个统一返回类
package com.eceibs.report.util;
/**
* http请求返回的最外层对象
* Created by wzh-zhua on 2018/10/1.
*/
public class Result<T> {
/** 错误码. */
private Integer code;
/** 提示信息. */
private String msg;
/** 具体的内容. */
private T data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
- 定义一个工具类
package com.eceibs.report.util;
public class ResultUtil {
public static Result success(Object object) {
Result result = new Result();
result.setCode(0);
result.setMsg("success");
result.setData(object);
return result;
}
public static Result success() {
return success(null);
}
public static Result error(Integer code, String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
}
- 调用
return ResultUtil.success(pageInfo);