完善 foodie-dev-api 模块下的 PassportController 类,添加 regist 接口。
package com.imooc.controller;import com.imooc.pojo.bo.UserBO;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();}@PostMapping("/regist")public IMOOCJSONResult regist(@RequestBody UserBO userBO) {String username = userBO.getUsername();String password = userBO.getPassword();String confirmPwd = userBO.getConfirmPassword();// 0. 判断用户名和密码不为空if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPwd)) {return IMOOCJSONResult.errorMsg("用户名或密码不能为空");}// 1. 查询用户名是否存在boolean isExist = userService.queryUsernameIsExist(username);if (isExist) {return IMOOCJSONResult.errorMsg("用户名已经存在");}// 2. 密码长度不能少于 6 位if (password.length() < 6) {return IMOOCJSONResult.errorMsg("密码长度不能少于6");}// 3. 判断两次密码是否一致if (!password.equals(confirmPwd)) {return IMOOCJSONResult.errorMsg("两次密码输入不一致");}// 4. 实现注册userService.createUser(userBO);return IMOOCJSONResult.ok();}}
启动项目,通过 Postman 调用 regist 接口,传入空 JSON Object,返回 500 错误
填写 JSON Object,请求接口,返回“两次密码输入不一致”
{"username": "imooc","password": "123123","confirmPassword": "123456"}

修改 JSON Object 内容,再次发送请求,返回 OK,查看数据库,发现插入一条记录
{
"username": "imooc",
"password": "123123",
"confirmPassword": "123123"
}


