@ApiIgnore:将类隐藏,不显示
    在 HelloController 类和 StuFooController 类中添加 @ApiIgnore 注解

    1. package com.imooc.controller;
    2. import com.imooc.service.StuService;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.PostMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import springfox.documentation.annotations.ApiIgnore;
    8. /**
    9. * @author 92578
    10. * @since 1.0
    11. */
    12. @ApiIgnore
    13. @RestController
    14. public class StuFooController {
    15. @Autowired
    16. private StuService stuService;
    17. @GetMapping("/getStu")
    18. public Object getStu(int id) {
    19. return stuService.getStuInfo(id);
    20. }
    21. @PostMapping("/saveStu")
    22. public Object saveStu() {
    23. stuService.saveStu();
    24. return "OK";
    25. }
    26. @PostMapping("/updateStu")
    27. public Object updateStu(int id) {
    28. stuService.updateStu(id);
    29. return "OK";
    30. }
    31. @PostMapping("/deleteStu")
    32. public Object deleteStu(int id) {
    33. stuService.deleteStu(id);
    34. return "OK";
    35. }
    36. }
    package com.imooc.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import springfox.documentation.annotations.ApiIgnore;
    
    /**
     * @author 92578
     * @since 1.0
     */
    @ApiIgnore
    @RestController
    public class HelloController {
    
        @GetMapping("/hello")
        public Object hello() {
            return "Hello World~";
        }
    }
    

    swagger 文档会不显示上面两个类
    image.png
    @Api:类的名称
    @ApiOperation(value = “接口名称”, notes = “接口描述”, httpMethod = “请求方式”)
    请求方式为 “GET”, “HEAD”, “POST”, “PUT”, “DELETE”, “OPTIONS” 和 “PATCH”,全部为大写。
    给 PassportController 类添加 @Api 注解和 @ApiOperation 注解

    package com.imooc.controller;
    
    import com.imooc.pojo.bo.UserBO;
    import com.imooc.service.UserService;
    import com.imooc.utils.IMOOCJSONResult;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    /**
     * @author 92578
     * @since 1.0
     */
    @Api(value = "注册登录", tags = {"用于注册登录的相关接口"})
    @RestController
    @RequestMapping("passport")
    public class PassportController {
    
        @Autowired
        private UserService userService;
    
        @ApiOperation(value = "用户名是否存在", notes = "用户名是否存在", httpMethod = "GET")
        @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();
        }
    
        @ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST")
        @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();
        }
    }
    

    类名称不在显示英文,而是中文
    image.png
    @ApiModel:在实体类上边使用,标记类是 swagger 的解析类
    @ApiModelProperty:使用在被 @ApiModel 注解的模型类的属性上,表示对 model 属性的说明或者数据操作更改
    在 UserBO 类中添加 @ApiModel 和 @ApiModelProperty

    package com.imooc.pojo.bo;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    
    /**
     * Created by 92578 on 2020/8/16 13:57
     **/
    @ApiModel(value = "用户对象BO", description = "从客户端,由用户传入的数据封装在此 entity 中")
    public class UserBO {
    
        @ApiModelProperty(value = "用户名", name = "用户名", example = "imooc", required = true)
        private String username;
        @ApiModelProperty(value = "密码", name = "密码", example = "123456", required = true)
        private String password;
        @ApiModelProperty(value = "确认密码", name = "确认密码", example = "123456", required = true)
        private String confirmPassword;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getConfirmPassword() {
            return confirmPassword;
        }
    
        public void setConfirmPassword(String confirmPassword) {
            this.confirmPassword = confirmPassword;
        }
    }
    

    请求参数不再显示英文,而是中文
    image.png