当前位置:   article > 正文

Restful Api 写法——统一返回值_restful api 统一返回值

restful api 统一返回值

记录Restful Api 自定义统一返回值方式!

以下为示例代码:

Code码Java类

  1. package *.*.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * API 统一返回状态码
  6. * Created by ws on 19/4/23.
  7. */
  8. public enum ResultCode {
  9. /* 成功状态码 */
  10. SUCCESS(1, "成功"),
  11. /* 参数错误:10001-19999 */
  12. PARAM_IS_INVALID(10001, "参数无效"),
  13. PARAM_IS_BLANK(10002, "参数为空"),
  14. PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
  15. PARAM_NOT_COMPLETE(10004, "参数缺失"),
  16. /* 用户错误:20001-29999*/
  17. USER_NOT_LOGGED_IN(20001, "用户未登录"),
  18. USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),
  19. USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
  20. USER_NOT_EXIST(20004, "用户不存在"),
  21. USER_HAS_EXISTED(20005, "用户已存在"),
  22. /* 业务错误:30001-39999 */
  23. SPECIFIED_QUESTIONED_USER_NOT_EXIST(30001, "某业务出现问题"),
  24. /* 系统错误:40001-49999 */
  25. SYSTEM_INNER_ERROR(40001, "系统繁忙,请稍后重试"),
  26. /* 数据错误:50001-599999 */
  27. RESULE_DATA_NONE(50001, "数据未找到"),
  28. DATA_IS_WRONG(50002, "数据有误"),
  29. DATA_ALREADY_EXISTED(50003, "数据已存在"),
  30. DATA_HAS_BEEN_DELETED(50004,"数据已被删除"),
  31. /* 接口错误:60001-69999 */
  32. INTERFACE_INNER_INVOKE_ERROR(60001, "内部系统接口调用异常"),
  33. INTERFACE_OUTTER_INVOKE_ERROR(60002, "外部系统接口调用异常"),
  34. INTERFACE_FORBID_VISIT(60003, "该接口禁止访问"),
  35. INTERFACE_ADDRESS_INVALID(60004, "接口地址无效"),
  36. INTERFACE_REQUEST_TIMEOUT(60005, "接口请求超时"),
  37. INTERFACE_EXCEED_LOAD(60006, "接口负载过高"),
  38. /* 权限错误:70001-79999 */
  39. PERMISSION_NO_ACCESS(70001, "无访问权限");
  40. private Integer code;
  41. private String message;
  42. ResultCode(Integer code, String message) {
  43. this.code = code;
  44. this.message = message;
  45. }
  46. public Integer code() {
  47. return this.code;
  48. }
  49. public String message() {
  50. return this.message;
  51. }
  52. public static String getMessage(String name) {
  53. for (ResultCode item : ResultCode.values()) {
  54. if (item.name().equals(name)) {
  55. return item.message;
  56. }
  57. }
  58. return name;
  59. }
  60. public static Integer getCode(String name) {
  61. for (ResultCode item : ResultCode.values()) {
  62. if (item.name().equals(name)) {
  63. return item.code;
  64. }
  65. }
  66. return null;
  67. }
  68. @Override
  69. public String toString() {
  70. return this.name();
  71. }
  72. //校验重复的code值
  73. public static void main(String[] args) {
  74. ResultCode[] ApiResultCodes = ResultCode.values();
  75. List<Integer> codeList = new ArrayList<>();
  76. for (ResultCode ApiResultCode : ApiResultCodes) {
  77. if (codeList.contains(ApiResultCode.code)) {
  78. System.out.println(ApiResultCode.code);
  79. } else {
  80. codeList.add(ApiResultCode.code());
  81. }
  82. }
  83. }
  84. }

 返回值Java类

  1. package *.*.*;
  2. import java.io.Serializable;
  3. /**
  4. * API统一返回值类
  5. * Created by ws on 19/4/23.
  6. */
  7. public class ResultMessage implements Serializable {
  8. private static final long serialVersionUID = 1L;
  9. private Integer code;
  10. private String msg;
  11. private Object data;
  12. public ResultMessage() {}
  13. public ResultMessage(Integer code, String msg) {
  14. this.code = code;
  15. this.msg = msg;
  16. }
  17. public static ResultMessage success() {
  18. ResultMessage result = new ResultMessage();
  19. result.setResultCode(ResultCode.SUCCESS);
  20. return result;
  21. }
  22. public static ResultMessage success(Object data) {
  23. ResultMessage result = new ResultMessage();
  24. result.setResultCode(ResultCode.SUCCESS);
  25. result.setData(data);
  26. return result;
  27. }
  28. public static ResultMessage failure(ResultCode resultCode) {
  29. ResultMessage result = new ResultMessage();
  30. result.setResultCode(resultCode);
  31. return result;
  32. }
  33. public static ResultMessage failure(ResultCode resultCode, Object data) {
  34. ResultMessage result = new ResultMessage();
  35. result.setResultCode(resultCode);
  36. result.setData(data);
  37. return result;
  38. }
  39. public void setResultCode(ResultCode code) {
  40. this.code = code.code();
  41. this.msg = code.message();
  42. }
  43. public Integer getCode() {
  44. return code;
  45. }
  46. public void setCode(Integer code) {
  47. this.code = code;
  48. }
  49. public String getMsg() {
  50. return msg;
  51. }
  52. public void setMsg(String msg) {
  53. this.msg = msg;
  54. }
  55. public Object getData() {
  56. return data;
  57. }
  58. public void setData(Object data) {
  59. this.data = data;
  60. }
  61. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/869856
推荐阅读
相关标签
  

闽ICP备14008679号