创建类 R.java
- 方法返回是是this ,可以实现链式编程。 | package com.wzy.commonutils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
//统一返回结果的类
@Data
public class R {
**private **R(){}_//私有的无参构造,不对外new对象
_@ApiModelProperty(value = **"是否成功"**)<br /> **private **Boolean **success**;
@ApiModelProperty(value = **"返回码"**)<br /> **private **Integer **code**;
@ApiModelProperty(value = **"返回消息"**)<br /> **private **String **message**;
@ApiModelProperty(value = **"返回数据"**)<br /> **private **Map<String, Object> **data **= **new **HashMap<String, Object>();
**public static **R ok(){<br /> R r = **new **R();<br /> r.setSuccess(**true**);<br /> r.setCode(ResultCode.**_SUCCESS_**);<br /> r.setMessage(**"成功"**);<br /> **return **r;<br /> }
**public static **R error(){<br /> R r = **new **R();<br /> r.setSuccess(**false**);<br /> r.setCode(ResultCode.**_ERROR_**);<br /> r.setMessage(**"失败"**);<br /> **return **r;<br /> }
**public **R success(Boolean success){<br /> **this**.setSuccess(success);<br /> **return this**;<br /> }
**public **R message(String message){<br /> **this**.setMessage(message);<br /> **return this**;<br /> }
**public **R code(Integer code){<br /> **this**.setCode(code);<br /> **return this**;<br /> }
**public **R data(String key, Object value){<br /> **this**.**data**.put(key, value);<br /> **return this**;<br /> }
**public **R data(Map<String, Object> map){<br /> **this**.setData(map);<br /> **return this**;<br /> }
}
| | —- |