Result
@Data
public class Result<T> {
private Integer code;
private String msg;
private Long count;
private T data;
}
ResultUtils
public class ResultUtils {
public static Result buildFail(Integer code,String msg){
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
/**
* 构建失败信息
* @param messageCode
* @return
*/
public static Result buildFail(MessageCode messageCode){
return buildFail(messageCode.getCode(),messageCode.getMsg());
}
/**
* 构建列表的请求成功
* @param data
* @param count
* @return
*/
public static Result buildSuccess(Object data,Long count){
Result result=new Result();
result.setCode(MessageCode.REQUEST_SUCCESS.getCode());
result.setMsg(MessageCode.REQUEST_SUCCESS.getMsg());
result.setData(data);
result.setCount(count);
return result;
}
/**
* 构建请求成功单个数据
* @param data
* @return
*/
public static Result buildSuccess(Object data){
return buildSuccess(data,null);
}
/**
* 构建没有携带数据的请求成功
* @return
*/
public static Result buildSuccess(){
return buildSuccess(null,null);
}
}
MessageCode
public enum MessageCode {
REQUEST_SUCCESS(0, "请求成功"),
SYSTEM_ERROR(50000, "对不起,系统开小差了!"),
SMS_EXPIRE(50005, "验证码失效!"),
SMS_ERROR(50006, "验证码输入错误!"),
NO_PERMISSION(50007, "你没有权限!"),
NOT_EXISTS_TOKEN(50008, "未携带token"),
INVALID_TOKEN(50009, "无效的token"),
FAIL_LOGIN(50010, "用户名或密码不正确!");
private Integer code;
private String msg;
private MessageCode(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
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;
}
}
实例:
@RestController
@RequestMapping("tUser")
public class TUserController {
/**
* 服务对象
*/
@Resource
private TUserService tUserService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("{id}")
public Result<TUser> queryById(@PathVariable("id") Integer id) {
return ResultUtils.buildSuccess(this.tUserService.queryById(id));
}