install 项目成功,如果失败请将 foodie-dev-api 下的 test 类全部注释
    image.png
    使用 Postman 调用 usernameIsExist 接口,返回 200
    image.png
    修改 PassportController 类,将返回的整数替换成定义好的枚举

    1. package com.imooc.controller;
    2. import com.imooc.service.UserService;
    3. import org.apache.commons.lang3.StringUtils;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.http.HttpStatus;
    6. import org.springframework.web.bind.annotation.*;
    7. /**
    8. * @author 92578
    9. * @since 1.0
    10. */
    11. @RestController
    12. @RequestMapping("passport")
    13. public class PassportController {
    14. @Autowired
    15. private UserService userService;
    16. @GetMapping("/usernameIsExist")
    17. public HttpStatus usernameIsExist(@RequestParam String username) {
    18. // 1. 判断用户名不能为空
    19. if (StringUtils.isBlank(username)) {
    20. return HttpStatus.INTERNAL_SERVER_ERROR;
    21. }
    22. // 2. 查找注册的用户名是否存在
    23. boolean isExist = userService.queryUsernameIsExist(username);
    24. if (isExist) {
    25. return HttpStatus.INTERNAL_SERVER_ERROR;
    26. }
    27. // 3. 请求成功,用户名没有重复
    28. return HttpStatus.OK;
    29. }
    30. }

    使用 Postman 调用 usernameIsExist 接口,返回 OK
    image.png
    在 foodie-dev-common 模块的 com.imooc.utils 包下面创建 IMOOCJSONResult 类

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

    修改 PassportController 类,将返回的枚举替换成自定义响应数据

    1. package com.imooc.controller;
    2. import com.imooc.service.UserService;
    3. import com.imooc.utils.IMOOCJSONResult;
    4. import org.apache.commons.lang3.StringUtils;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.*;
    7. /**
    8. * @author 92578
    9. * @since 1.0
    10. */
    11. @RestController
    12. @RequestMapping("passport")
    13. public class PassportController {
    14. @Autowired
    15. private UserService userService;
    16. @GetMapping("/usernameIsExist")
    17. public IMOOCJSONResult usernameIsExist(@RequestParam String username) {
    18. // 1. 判断用户名不能为空
    19. if (StringUtils.isBlank(username)) {
    20. return IMOOCJSONResult.errorMsg("用户名不能为空");
    21. }
    22. // 2. 查找注册的用户名是否存在
    23. boolean isExist = userService.queryUsernameIsExist(username);
    24. if (isExist) {
    25. return IMOOCJSONResult.errorMsg("用户名已经存在");
    26. }
    27. // 3. 请求成功,用户名没有重复
    28. return IMOOCJSONResult.ok();
    29. }
    30. }

    install 后重启项目,使用 Postman 调用 usernameIsExist 接口,返回自定义响应信息
    image.png
    删除 username 值,发送请求返回 500
    image.png