认清现实,放弃幻想,准备斗争
{
"code": 0,
"message": "成功",
"data": 数据
}
{
"code": -1,
"message": "失败",
"data": null
}
因此,我们定义统一结果
{
"code": 数字, //业务响应码
"message": 字符串, //返回消息
"data": 对象 //返回数据
}
@Getter
@AllArgsConstructor
@ToString
public enum ResponseEnum {
SUCCESS(0, "成功"),
ERROR(-1, "服务器内部错误"),
;
// 响应状态码
private Integer code;
// 响应信息
private String message;
}
完整版本
@Getter
@AllArgsConstructor
@ToString
public enum ResponseEnum {
SUCCESS(0,"成功"),
ERROR(-1,"服务器内部乘错误"),
//-1xx 服务器错误
BAD_SQL_GRAMMAR_ERROR(-101, "sql语法错误"),
SERVLET_ERROR(-102, "servlet请求异常"), //-2xx 参数校验
UPLOAD_ERROR(-103, "文件上传错误"),
EXPORT_DATA_ERROR(104, "数据导出失败"),
//-2xx 参数校验
BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
MOBILE_NULL_ERROR(-202, "手机号码不能为空"),
MOBILE_ERROR(-203, "手机号码不正确"),
PASSWORD_NULL_ERROR(204, "密码不能为空"),
CODE_NULL_ERROR(205, "验证码不能为空"),
CODE_ERROR(206, "验证码错误"),
MOBILE_EXIST_ERROR(207, "手机号已被注册"),
LOGIN_MOBILE_ERROR(208, "用户不存在"),
LOGIN_PASSWORD_ERROR(209, "密码错误"),
LOGIN_LOKED_ERROR(210, "用户被锁定"),
LOGIN_AUTH_ERROR(-211, "未登录"),
USER_BIND_IDCARD_EXIST_ERROR(-301, "身份证号码已绑定"),
USER_NO_BIND_ERROR(302, "用户未绑定"),
USER_NO_AMOUNT_ERROR(303, "用户信息未审核"),
USER_AMOUNT_LESS_ERROR(304, "您的借款额度不足"),
LEND_INVEST_ERROR(305, "当前状态无法投标"),
LEND_FULL_SCALE_ERROR(306, "已满标,无法投标"),
NOT_SUFFICIENT_FUNDS_ERROR(307, "余额不足,请充值"),
PAY_UNIFIEDORDER_ERROR(401, "统一下单错误"),
ALIYUN_SMS_LIMIT_CONTROL_ERROR(-502, "短信发送过于频繁"),//业务限流
ALIYUN_SMS_ERROR(-503, "短信发送失败"),//其他失败
WEIXIN_CALLBACK_PARAM_ERROR(-601, "回调参数不正确"),
WEIXIN_FETCH_ACCESSTOKEN_ERROR(-602, "获取access_token失败"),
WEIXIN_FETCH_USERINFO_ERROR(-603, "获取用户信息失败");
//响应状态码
private Integer code;
//响应信息
private String message;
}
@Data
public class R {
private Integer code;
private String message;
private Map data = new HashMap();
/**
* 构造器私有
*/
private R(){}
/**
* 返回成功
*/
public static R ok(){
R r = new R();
r.setCode(ResponseEnum.SUCCESS.getCode());
r.setMessage(ResponseEnum.SUCCESS.getMessage());
return r;
}
/**
* 返回失败
*/
public static R error(){
R r = new R();
r.setCode(ResponseEnum.ERROR.getCode());
r.setMessage(ResponseEnum.ERROR.getMessage());
return r;
}
/**
* 设置特定结果
*/
public static R setResult(ResponseEnum responseEnum){
R r = new R();
r.setCode(responseEnum.getCode());
r.setMessage(responseEnum.getMessage());
return r;
}
public R message(String message){
this.setMessage(message);
return this;
}
public R code(Integer code){
this.setCode(code);
return this;
}
public R data(String key, Object value){
this.data.put(key, value);
return this;
}
public R data(Map map){
this.setData(map);
return this;
}
}
这里采用了链式编程思想
@ApiOperation("积分等级列表")
@GetMapping("/list")
public R listAll(){
List list = integralGradeService.list();
return R.ok().data("list", list);
}
@ApiOperation(value = "根据id删除积分等级", notes="逻辑删除")
@DeleteMapping("/remove/{id}")
public R removeById(
@ApiParam(value = "数据id", required = true, example = "1")
@PathVariable Long id){
boolean result = integralGradeService.removeById(id);
if(result){
//return R.setResult(ResponseEnum.UPLOAD_ERROR);
return R.ok().message("删除成功");
}else{
return R.error().message("删除失败");
}
}
@ApiOperation("新增积分等级")
@PostMapping("/save")
public R save(
@ApiParam(value = "积分等级对象", required = true)
@RequestBody IntegralGrade integralGrade){
boolean result = integralGradeService.save(integralGrade);
if (result) {
return R.ok().message("保存成功");
} else {
return R.error().message("保存失败");
}
}
@ApiOperation("根据id获取积分等级")
@GetMapping("/get/{id}")
public R getById(
@ApiParam(value = "数据id", required = true, example = "1")
@PathVariable Long id
){
IntegralGrade integralGrade = integralGradeService.getById(id);
if(integralGrade != null){
return R.ok().data("record", integralGrade);
}else{
return R.error().message("数据不存在");
}
}
@ApiOperation("更新积分等级")
@PutMapping("/update")
public R updateById(
@ApiParam(value = "积分等级对象", required = true)
@RequestBody IntegralGrade integralGrade){
boolean result = integralGradeService.updateById(integralGrade);
if(result){
return R.ok().message("修改成功");
}else{
return R.error().message("修改失败");
}
}