install 项目成功,如果失败请将 foodie-dev-api 下的 test 类全部注释
使用 Postman 调用 usernameIsExist 接口,返回 200
修改 PassportController 类,将返回的整数替换成定义好的枚举
package com.imooc.controller;import com.imooc.service.UserService;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.*;/*** @author 92578* @since 1.0*/@RestController@RequestMapping("passport")public class PassportController {@Autowiredprivate UserService userService;@GetMapping("/usernameIsExist")public HttpStatus usernameIsExist(@RequestParam String username) {// 1. 判断用户名不能为空if (StringUtils.isBlank(username)) {return HttpStatus.INTERNAL_SERVER_ERROR;}// 2. 查找注册的用户名是否存在boolean isExist = userService.queryUsernameIsExist(username);if (isExist) {return HttpStatus.INTERNAL_SERVER_ERROR;}// 3. 请求成功,用户名没有重复return HttpStatus.OK;}}
使用 Postman 调用 usernameIsExist 接口,返回 OK
在 foodie-dev-common 模块的 com.imooc.utils 包下面创建 IMOOCJSONResult 类
package com.imooc.utils;import com.fasterxml.jackson.annotation.JsonIgnore;import com.fasterxml.jackson.databind.ObjectMapper;/*** 自定义响应数据结构* 本类可提供给 H5/ios/安卓/公众号/小程序 使用* 前端接受此类数据(json object)后,可自行根据业务去实现相关功能* 200:表示成功* 500:表示错误,错误信息在 msg 字段中* 501:bean 验证错误,不管多少个错误都以 map 形式返回* 502:拦截器拦截到用户 token 出错* 555:异常抛出信息* 556: 用户 qq 校验异常* Created by 92578 on 2020/8/16 0:31**/public class IMOOCJSONResult {// 定义jackson对象private static final ObjectMapper MAPPER = new ObjectMapper();// 响应业务状态private Integer status;// 响应消息private String msg;// 响应中的数据private Object data;@JsonIgnoreprivate String ok; // 不使用public static IMOOCJSONResult build(Integer status, String msg, Object data) {return new IMOOCJSONResult(status, msg, data);}public static IMOOCJSONResult build(Integer status, String msg, Object data, String ok) {return new IMOOCJSONResult(status, msg, data, ok);}public static IMOOCJSONResult ok(Object data) {return new IMOOCJSONResult(data);}public static IMOOCJSONResult ok() {return new IMOOCJSONResult(null);}public static IMOOCJSONResult errorMsg(String msg) {return new IMOOCJSONResult(500, msg, null);}public static IMOOCJSONResult errorMap(Object data) {return new IMOOCJSONResult(501, "error", data);}public static IMOOCJSONResult errorTokenMsg(String msg) {return new IMOOCJSONResult(502, msg, null);}public static IMOOCJSONResult errorException(String msg) {return new IMOOCJSONResult(555, msg, null);}public static IMOOCJSONResult errorUserQQ(String msg) {return new IMOOCJSONResult(556, msg, null);}public IMOOCJSONResult() {}public IMOOCJSONResult(Integer status, String msg, Object data) {this.status = status;this.msg = msg;this.data = data;}public IMOOCJSONResult(Integer status, String msg, Object data, String ok) {this.status = status;this.msg = msg;this.data = data;this.ok = ok;}public IMOOCJSONResult(Object data) {this.status = 200;this.msg = "OK";this.data = data;}public Boolean isOK() {return this.status == 200;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public String getOk() {return ok;}public void setOk(String ok) {this.ok = ok;}}
修改 PassportController 类,将返回的枚举替换成自定义响应数据
package com.imooc.controller;import com.imooc.service.UserService;import com.imooc.utils.IMOOCJSONResult;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;/*** @author 92578* @since 1.0*/@RestController@RequestMapping("passport")public class PassportController {@Autowiredprivate UserService userService;@GetMapping("/usernameIsExist")public IMOOCJSONResult usernameIsExist(@RequestParam String username) {// 1. 判断用户名不能为空if (StringUtils.isBlank(username)) {return IMOOCJSONResult.errorMsg("用户名不能为空");}// 2. 查找注册的用户名是否存在boolean isExist = userService.queryUsernameIsExist(username);if (isExist) {return IMOOCJSONResult.errorMsg("用户名已经存在");}// 3. 请求成功,用户名没有重复return IMOOCJSONResult.ok();}}
install 后重启项目,使用 Postman 调用 usernameIsExist 接口,返回自定义响应信息
删除 username 值,发送请求返回 500
