1.Object

    1. @ToString
    2. @Data
    3. @NoArgsConstructor
    4. @AllArgsConstructor
    5. @Accessors(chain = true)
    6. public class R<T> implements Serializable {
    7. private static final long serialVersionUID = 1L;
    8. @ApiModelProperty("状态码,0:成功,1:失败")
    9. private int code;
    10. @ApiModelProperty("返回信息")
    11. private String msg;
    12. @ApiModelProperty("返回数据")
    13. private T data;
    14. public static R ok(Object data) {
    15. return new R(ResponseConstants.SUCCESS,"success",data);
    16. }
    17. public static R ok(int code, String msg, Object data) {
    18. return new R().setStatus(code).setMsg(msg).setData(data);
    19. }
    20. public static R fail(Object data) {
    21. return new R(ResponseConstants.FAIL, "error",data);
    22. }
    23. public static R fail(int code, String msg, Object data) {
    24. return new R().setStatus(code).setMsg(msg).setData(data);
    25. }
    26. }

    2.范型T

    1. @ToString
    2. @Data
    3. @NoArgsConstructor
    4. @AllArgsConstructor
    5. @Accessors(chain = true)
    6. public class R<T> implements Serializable {
    7. private static final long serialVersionUID = 1L;
    8. @ApiModelProperty("状态码,0:成功,1:失败")
    9. private int code;
    10. @ApiModelProperty("返回信息")
    11. private String msg;
    12. @ApiModelProperty("返回数据")
    13. private T data;
    14. public static <T> R<T> ok(T data) {
    15. return new R(ResponseConstants.SUCCESS, "", data);
    16. }
    17. public static <T> R<T> ok(Integer code, String msg, T data) {
    18. return new R(code, msg, data);
    19. }
    20. public static <T> R<T> fail(T data) {
    21. return new R(ResponseConstants.FAIL, "", data);
    22. }
    23. public static <T> R<T> fail(Integer code, String msg, T data) {
    24. return new R(code, msg, data);
    25. }
    26. }