完善 foodie-dev-api 模块下的 PassportController 类,添加 regist 接口。

    1. package com.imooc.controller;
    2. import com.imooc.pojo.bo.UserBO;
    3. import com.imooc.service.UserService;
    4. import com.imooc.utils.IMOOCJSONResult;
    5. import org.apache.commons.lang3.StringUtils;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.web.bind.annotation.*;
    8. /**
    9. * @author 92578
    10. * @since 1.0
    11. */
    12. @RestController
    13. @RequestMapping("passport")
    14. public class PassportController {
    15. @Autowired
    16. private UserService userService;
    17. @GetMapping("/usernameIsExist")
    18. public IMOOCJSONResult usernameIsExist(@RequestParam String username) {
    19. // 1. 判断用户名不能为空
    20. if (StringUtils.isBlank(username)) {
    21. return IMOOCJSONResult.errorMsg("用户名不能为空");
    22. }
    23. // 2. 查找注册的用户名是否存在
    24. boolean isExist = userService.queryUsernameIsExist(username);
    25. if (isExist) {
    26. return IMOOCJSONResult.errorMsg("用户名已经存在");
    27. }
    28. // 3. 请求成功,用户名没有重复
    29. return IMOOCJSONResult.ok();
    30. }
    31. @PostMapping("/regist")
    32. public IMOOCJSONResult regist(@RequestBody UserBO userBO) {
    33. String username = userBO.getUsername();
    34. String password = userBO.getPassword();
    35. String confirmPwd = userBO.getConfirmPassword();
    36. // 0. 判断用户名和密码不为空
    37. if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(confirmPwd)) {
    38. return IMOOCJSONResult.errorMsg("用户名或密码不能为空");
    39. }
    40. // 1. 查询用户名是否存在
    41. boolean isExist = userService.queryUsernameIsExist(username);
    42. if (isExist) {
    43. return IMOOCJSONResult.errorMsg("用户名已经存在");
    44. }
    45. // 2. 密码长度不能少于 6 位
    46. if (password.length() < 6) {
    47. return IMOOCJSONResult.errorMsg("密码长度不能少于6");
    48. }
    49. // 3. 判断两次密码是否一致
    50. if (!password.equals(confirmPwd)) {
    51. return IMOOCJSONResult.errorMsg("两次密码输入不一致");
    52. }
    53. // 4. 实现注册
    54. userService.createUser(userBO);
    55. return IMOOCJSONResult.ok();
    56. }
    57. }

    启动项目,通过 Postman 调用 regist 接口,传入空 JSON Object,返回 500 错误
    image.png
    填写 JSON Object,请求接口,返回“两次密码输入不一致”

    1. {
    2. "username": "imooc",
    3. "password": "123123",
    4. "confirmPassword": "123456"
    5. }

    image.png
    修改 JSON Object 内容,再次发送请求,返回 OK,查看数据库,发现插入一条记录

    {
        "username": "imooc",
        "password": "123123",
        "confirmPassword": "123123"
    }
    

    image.png
    image.png