定义一个公共的类 写一个class

    1. package com.utils;
    2. import com.fasterxml.jackson.annotation.JsonIgnore;
    3. import com.fasterxml.jackson.databind.ObjectMapper;
    4. /**
    5. *
    6. * @Title: IMOOCJSONResult.java
    7. * @Package com.imooc.utils
    8. * @Description: 自定义响应数据结构
    9. * 本类可提供给 H5/ios/安卓/公众号/小程序 使用
    10. * 前端接受此类数据(json object)后,可自行根据业务去实现相关功能
    11. *
    12. * 200:表示成功
    13. * 500:表示错误,错误信息在msg字段中
    14. * 501:bean验证错误,不管多少个错误都以map形式返回
    15. * 502:拦截器拦截到用户token出错
    16. * 555:异常抛出信息
    17. * 556: 用户qq校验异常*/
    18. public class IMOOCJSONResult {
    19. // 定义jackson对象
    20. private static final ObjectMapper MAPPER = new ObjectMapper();
    21. // 响应业务状态
    22. private Integer status;
    23. // 响应消息
    24. private String msg;
    25. // 响应中的数据
    26. private Object data;
    27. @JsonIgnore
    28. private String ok; // 不使用
    29. public static IMOOCJSONResult build(Integer status, String msg, Object data) {
    30. return new IMOOCJSONResult(status, msg, data);
    31. }
    32. public static IMOOCJSONResult build(Integer status, String msg, Object data, String ok) {
    33. return new IMOOCJSONResult(status, msg, data, ok);
    34. }
    35. public static IMOOCJSONResult ok(Object data) {
    36. return new IMOOCJSONResult(data);
    37. }
    38. public static IMOOCJSONResult ok() {
    39. return new IMOOCJSONResult(null);
    40. }
    41. public static IMOOCJSONResult errorMsg(String msg) {
    42. return new IMOOCJSONResult(500, msg, null);
    43. }
    44. public static IMOOCJSONResult errorMap(Object data) {
    45. return new IMOOCJSONResult(501, "error", data);
    46. }
    47. public static IMOOCJSONResult errorTokenMsg(String msg) {
    48. return new IMOOCJSONResult(502, msg, null);
    49. }
    50. public static IMOOCJSONResult errorException(String msg) {
    51. return new IMOOCJSONResult(555, msg, null);
    52. }
    53. public static IMOOCJSONResult errorUserQQ(String msg) {
    54. return new IMOOCJSONResult(556, msg, null);
    55. }
    56. public IMOOCJSONResult() {
    57. }
    58. public IMOOCJSONResult(Integer status, String msg, Object data) {
    59. this.status = status;
    60. this.msg = msg;
    61. this.data = data;
    62. }
    63. public IMOOCJSONResult(Integer status, String msg, Object data, String ok) {
    64. this.status = status;
    65. this.msg = msg;
    66. this.data = data;
    67. this.ok = ok;
    68. }
    69. public IMOOCJSONResult(Object data) {
    70. this.status = 200;
    71. this.msg = "OK";
    72. this.data = data;
    73. }
    74. public Boolean isOK() {
    75. return this.status == 200;
    76. }
    77. public Integer getStatus() {
    78. return status;
    79. }
    80. public void setStatus(Integer status) {
    81. this.status = status;
    82. }
    83. public String getMsg() {
    84. return msg;
    85. }
    86. public void setMsg(String msg) {
    87. this.msg = msg;
    88. }
    89. public Object getData() {
    90. return data;
    91. }
    92. public void setData(Object data) {
    93. this.data = data;
    94. }
    95. public String getOk() {
    96. return ok;
    97. }
    98. public void setOk(String ok) {
    99. this.ok = ok;
    100. }
    101. }

    使用它:先引入

    1. @Autowired
    2. private UserService userService;
    3. @GetMapping("/usernameIsExist")
    4. public IMOOCJSONResult usernameIsExist(@RequestParam String username) {
    5. // 1. 判断用户名不能为空
    6. if (StringUtils.isBlank(username)) {
    7. return IMOOCJSONResult.errorMsg("用户名不能为");
    8. }
    9. // 2. 查找注册的用户名是否存在
    10. boolean isExist = userService.queryUsernameIsExist(username);
    11. if (isExist) {
    12. return IMOOCJSONResult.errorMsg("用户名已经存在");
    13. }
    14. // 3. 请求成功,用户名没有重复
    15. return IMOOCJSONResult.ok();
    16. }

    postman 测试:

    spring boot  自定义响应数据结构 - 图1