创建类 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 {

    1. **private **R(){}_//私有的无参构造,不对外new对象
    2. _@ApiModelProperty(value = **"是否成功"**)<br /> **private **Boolean **success**;
    3. @ApiModelProperty(value = **"返回码"**)<br /> **private **Integer **code**;
    4. @ApiModelProperty(value = **"返回消息"**)<br /> **private **String **message**;
    5. @ApiModelProperty(value = **"返回数据"**)<br /> **private **Map<String, Object> **data **= **new **HashMap<String, Object>();
    6. **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 /> }
    7. **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 /> }
    8. **public **R success(Boolean success){<br /> **this**.setSuccess(success);<br /> **return this**;<br /> }
    9. **public **R message(String message){<br /> **this**.setMessage(message);<br /> **return this**;<br /> }
    10. **public **R code(Integer code){<br /> **this**.setCode(code);<br /> **return this**;<br /> }
    11. **public **R data(String key, Object value){<br /> **this**.**data**.put(key, value);<br /> **return this**;<br /> }
    12. **public **R data(Map<String, Object> map){<br /> **this**.setData(map);<br /> **return this**;<br /> }

    }

    | | —- |