人狠话不多,直接上代码,希望你喜欢.
    ||
    ||

    实体类:Result

    1. package xxxx.xxxx.entity;
    2. import xxx.enums.ResultCode;
    3. import lombok.Data;
    4. import java.io.Serializable;
    5. /**
    6. * API 统一返回状态码
    7. * Created by cxz on 18/7/12
    8. */
    9. @Data
    10. public class Result implements Serializable {
    11. private static final long serialVersionUID = -3948389268046368059L;
    12. private Integer code;
    13. private String msg;
    14. private Object data;
    15. public Result() {}
    16. public Result(Integer code, String msg) {
    17. this.code = code;
    18. this.msg = msg;
    19. }
    20. /**
    21. * 返回成功状态码
    22. * 无数据
    23. * @return SUCCESS
    24. */
    25. public static Result success() {
    26. Result result = new Result();
    27. result.setResultCode(ResultCode.SUCCESS);
    28. return result;
    29. }
    30. /**
    31. * 返回成功状态码
    32. * @return SUCCESS
    33. */
    34. public static Result success(Object data) {
    35. Result result = new Result();
    36. result.setResultCode(ResultCode.SUCCESS);
    37. result.setData(data);
    38. return result;
    39. }
    40. /**
    41. * 返回成功状态码
    42. * @return SUCCESS
    43. */
    44. public static Result imgSuccess(Object data) {
    45. Result result = new Result();
    46. result.setResultCode(ResultCode.IMAGE_UPLOAD_SUCCESS);
    47. result.setData(data);
    48. return result;
    49. }
    50. /**
    51. * 返回失败状态码
    52. * @return FAILURE
    53. */
    54. public static Result imgFailure(Object data) {
    55. Result result = new Result();
    56. result.setResultCode(ResultCode.IMAGE_UPLOAD_ERROR);
    57. result.setData(data);
    58. return result;
    59. }
    60. /**
    61. * 返回成功状态码
    62. * 不是图片类型
    63. * @return FAILURE
    64. */
    65. public static Result noImageType(Object data) {
    66. Result result = new Result();
    67. result.setResultCode(ResultCode.NO_IMAGE_UPLOAD_TYPE);
    68. result.setData(data);
    69. return result;
    70. }
    71. /**
    72. * 添加成功状态码
    73. * 无数据
    74. * @return INSERT
    75. */
    76. public static Result insertSuccess() {
    77. Result result = new Result();
    78. result.setResultCode(ResultCode.DATA_INSERT_SUCCESS);
    79. return result;
    80. }
    81. /**
    82. * 更新成功状态码
    83. * 无数据
    84. * @return UPDATE
    85. */
    86. public static Result updateSuccess() {
    87. Result result = new Result();
    88. result.setResultCode(ResultCode.DATA_UPDATE_SUCCESS);
    89. return result;
    90. }
    91. /**
    92. * 删除成功状态码
    93. * 无数据
    94. * @return DELETE
    95. */
    96. public static Result deleteSuccess() {
    97. Result result = new Result();
    98. result.setResultCode(ResultCode.DATA_DELETE_SUCCESS);
    99. return result;
    100. }
    101. /**
    102. * 返回失败状态码
    103. * 无数据
    104. * @return FAILURE
    105. */
    106. public static Result failure(ResultCode resultCode) {
    107. Result result = new Result();
    108. result.setResultCode(resultCode);
    109. return result;
    110. }
    111. /**
    112. * 返回失败状态码
    113. * @return FAILURE
    114. */
    115. public static Result failure(ResultCode resultCode, Object data) {
    116. Result result = new Result();
    117. result.setResultCode(resultCode);
    118. result.setData(data);
    119. return result;
    120. }
    121. /**
    122. * 返回失败状态码
    123. * @return FAIL
    124. */
    125. public static Result fail(ResultCode resultCode, Object data) {
    126. Result result = new Result();
    127. result.setResultCode(resultCode);
    128. result.setData(data);
    129. return result;
    130. }
    131. /**
    132. * 添加失败状态码
    133. * 无数据
    134. * @return INSERT
    135. */
    136. public static Result insertFailure() {
    137. Result result = new Result();
    138. result.setResultCode(ResultCode.DATA_INSERT_FAILURE);
    139. return result;
    140. }
    141. /**
    142. * 更新失败状态码
    143. * 无数据
    144. * @return UPDATE
    145. */
    146. public static Result updateFailure() {
    147. Result result = new Result();
    148. result.setResultCode(ResultCode.DATA_UPDATE_FAILURE);
    149. return result;
    150. }
    151. /**
    152. * 删除失败状态码
    153. * 无数据
    154. * @return DELETE
    155. */
    156. public static Result deleteFailure() {
    157. Result result = new Result();
    158. result.setResultCode(ResultCode.DATA_DELETE_FAILURE);
    159. return result;
    160. }
    161. /**
    162. * 返回数据
    163. * @return data
    164. */
    165. private void setData(Object data) {
    166. this.data = data;
    167. }
    168. /**
    169. * 返回值
    170. * @return code
    171. * @return message
    172. */
    173. public void setResultCode(ResultCode code) {
    174. this.code = code.code();
    175. this.msg = code.message();
    176. }
    177. }

    接下来看枚举类型的工具类: ResultCode 类 不多少直接上代码
    该类中的数字大家统一修改啊 我的是根据自己的实际业务而自定义

    1. package xxx.enums;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. /**
    5. * API 统一返回状态码
    6. * Created by cxz on 18/7/12
    7. */
    8. public enum ResultCode {
    9. /* 成功状态码 */
    10. SUCCESS(1, "返回数据成功"),
    11. /* 增删改查 100001-100009*/
    12. DATA_INSERT_SUCCESS(1,"添加数据成功"),
    13. DATA_UPDATE_SUCCESS(1,"更新数据成功"),
    14. DATA_DELETE_SUCCESS(1,"删除数据成功"),
    15. DATA_BE_INTERRELATED(100013,"数据关联,无法删除"),
    16. ........省略N
    17. /* 参数错误:200001-299999 */
    18. PARAM_IS_INVALID(200001, "参数无效"),
    19. PARAM_IS_BLANK(200002, "参数为空"),
    20. ........省略N
    21. /* 用户错误:300001-399999*/
    22. USER_NOT_LOGGED_IN(300001, "用户未登录"),
    23. USER_LOGIN_ERROR(300002, "账号不存在或密码错误"),
    24. USER_ACCOUNT_FORBIDDEN(300003, "账号已被禁用"),
    25. USER_NOT_EXIST(300004, "用户不存在"),
    26. USER_HAS_EXISTED(300005, "用户已存在"),
    27. ........省略N
    28. /* 业务错误:400001-499999 */
    29. SPECIFIED_QUESTIONED_USER_NOT_EXIST(400001, "某业务出现问题"),
    30. /* 系统错误:500001-599999 */
    31. SYSTEM_INNER_ERROR(500001, "系统繁忙,请稍后重试"),
    32. /* 数据错误:600001-6999999 */
    33. RESTFUL_DATA_NONE(600001, "数据未找到"),
    34. DATA_IS_WRONG(600002, "数据有误"),
    35. DATA_ALREADY_EXISTED(600003, "数据已存在"),
    36. /* 接口错误:700001-799999 */
    37. INTERFACE_INNER_INVOKE_ERROR(700001, "内部系统接口调用异常"),
    38. INTERFACE_OUTER_INVOKE_ERROR(700002, "外部系统接口调用异常"),
    39. INTERFACE_FORBID_VISIT(700003, "该接口禁止访问"),
    40. INTERFACE_ADDRESS_INVALID(700004, "接口地址无效"),
    41. INTERFACE_REQUEST_TIMEOUT(700005, "接口请求超时"),
    42. INTERFACE_EXCEED_LOAD(700006, "接口负载过高"),
    43. INTERFACE_HEADER_DATA(700007, "接口调用失败"),
    44. ........省略N
    45. /* 权限错误:800001-899999 */
    46. PERMISSION_NO_ACCESS(800001, "无访问权限"),
    47. /* 图片上传:1001-1999 */
    48. IMAGE_UPLOAD_ERROR(1001, "图片上传失败"),
    49. NO_IMAGE_UPLOAD_TYPE(1002, "上传的文件不是图片类型,请重新上传"),
    50. IMAGE_UPLOAD_FAILURE(1003, "上传失败,请选择要上传的图片"),
    51. /* 图片上传:2001-2999 */
    52. IMAGE_UPLOAD_SUCCESS(2001, "图片上传成功");
    53. private Integer code;
    54. private String message;
    55. ResultCode(Integer code, String message) {
    56. this.code = code;
    57. this.message = message;
    58. }
    59. public Integer code() {
    60. return this.code;
    61. }
    62. public String message() {
    63. return this.message;
    64. }
    65. /**
    66. * 返回信息
    67. * @return NAME
    68. */
    69. public static String getMessage(String name) {
    70. for (ResultCode item : ResultCode.values()) {
    71. if (item.name().equals(name)) {
    72. return item.message;
    73. }
    74. }
    75. return name;
    76. }
    77. /**
    78. * 返回信息
    79. * @return CODE
    80. */
    81. public static Integer getCode(String name) {
    82. for (ResultCode item : ResultCode.values()) {
    83. if (item.name().equals(name)) {
    84. return item.code;
    85. }
    86. }
    87. return null;
    88. }
    89. @Override
    90. public String toString() {
    91. return this.name();
    92. }
    93. /**
    94. * 校验重复的code值
    95. * @return
    96. */
    97. public static void main(String[] args) {
    98. ResultCode[] ApiResultCodes = ResultCode.values();
    99. List<Integer> codeList = new ArrayList<Integer>();
    100. for (ResultCode ApiResultCode : ApiResultCodes) {
    101. if (codeList.contains(ApiResultCode.code)) {
    102. System.out.println(ApiResultCode.code);
    103. } else {
    104. codeList.add(ApiResultCode.code());
    105. }
    106. }
    107. }
    108. }

    接下来看最终的使用 都是干货 😄😄😄😄😄😄

    1. if( 查询出来的数据 != null){
    2. return Result.success(products);
    3. return Result.success();
    4. return Result.deleteSuccess()
    5. ....这里支持好几种返回 都在实体类中封装了 可以自行查看和使用
    6. }else{
    7. return Result.failure(ResultCode.RESTFUL_DATA_NONE);
    8. return Result.failure(ResultCode.RESTFUL_DATA_NONE,data);
    9. return Result.insertFailure()
    10. ... 失败的也有好几种方法我这里就不举栗了
    11. }

    好吧 再来张图
    Java 通用的接口统一返回格式 - 图1