• SpringBoot整合Swagger


    内容参考:https://www.cnblogs.com/byuan/p/14988295.html

    一,项目完整目录如下

    在这里插入图片描述

    二、在pom文件中引入相关依赖

      
    	
    	    io.springfox
    	    springfox-swagger2
    	    2.9.2
    	
    	
    	
    	    io.springfox
    	    springfox-swagger-ui
    	    2.9.2
    	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    三、创建Swagger的配置类,并进行配置

    package com.swagger.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * Swagger的配置类
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    //为当前包路径,控制器类包
                    .apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
        //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    //页面标题
                    .title("Spring Boot 集成 Swagger2 测试接口文档")
                    //创建人
                    .contact(new Contact("王晓明", "http://www.beidu.com", "2465293546@qq.com"))
                    //版本号
                    .version("1.0")
                    //描述
                    .description("API 描述")
                    .build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    四、发布项目,打开浏览器访问swagger的ui进行测试

    http://localhost:8080/swagger-ui.html
    在这里插入图片描述
    注:如果项目启动报错
    在这里插入图片描述
    这里使用的Swagger版本是2.9.2、springboot 版本是2.6.3
    发现是springboot版本太高,缺少swagger运行所需要的环境,回退到之前的版本
    把springboot回退到2.5.6即可正常启动

    五、创建实体类

    package com.swagger.entity;
    
    import io.swagger.annotations.ApiModelProperty;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    
    import java.io.Serializable;
    
    /**
     * 实体类
     */
    @NoArgsConstructor// 生成无参的构造方法
    @AllArgsConstructor// 生成满参的构造方法
    @Accessors(chain = true)// 使用链式调用
    @Data// 自动生成get/set方法、重写toString方法等方法
    public class Student implements Serializable {
        @ApiModelProperty(value = "学生id")// 对属性进行简要说明
        private Integer studentId;
    
        @ApiModelProperty(value = "学生姓名")
        private String studentName;
    
        @ApiModelProperty(value = "学生分数")
        private Double studentScore;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    六、创建vo,对返回结果进行封装

    package com.swagger.vo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    
    /**
     * 定义一个返回结果类
     */
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    @Accessors(chain = true)
    public class ResponseVo {
        private String message; //操作的提示信息
        private Integer status; //响应状态码
        private E data; //获取数据
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    七、创建service层

    package com.swagger.service;
    
    import com.swagger.entity.Student;
    import com.swagger.vo.ResponseVo;
    import org.springframework.stereotype.Service;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Service
     */
    @Service
    public class StudentService {
        // 这里我们不使用数据库, 使用Map集合来模拟数据库中的表
        private static Map studentMap=new HashMap<>();
        private static Integer studentId=10001;
        static {
            studentMap.put(studentId, new Student(studentId, "王晓明", 98.5));
            studentId++;
            studentMap.put(studentId, new Student(studentId, "王晓晓", 95.5));
            studentId++;
            studentMap.put(studentId, new Student(studentId, "王晓华", 96.5));
            studentId++;
        }
    
        /**
         * 插入一名学生返回影响行数
         * @param student
         * @return
         */
        public ResponseVo addOneStudent(Student student){
            student.setStudentId(studentId);
            studentMap.put(studentId, student);
            studentId++;
            return new ResponseVo<>("插入一条数据成功", 200, 1);
        }
    
        /**
         * 删除一位学生返回影响行数
         * @param studentId
         * @return
         */
        public  ResponseVo deleteOneStudentByStudentId(Integer studentId){
            if(studentMap.containsKey(studentId) == false){
                return new ResponseVo<>("您输入的id不存在", 200, 0);
            }
            studentMap.remove(studentId);
            return new ResponseVo<>("删除成功", 200, 1);
        }
    
        /**
         * 修改一位学生返回影响行数
         * @param student
         * @return
         */
        public ResponseVo updateOneStudent(Student student){
            if(studentMap.containsKey(student.getStudentId()) == false){
                return new ResponseVo<>("根据学生id,您所修改的学生不存在", 200, 0);
            }
            studentMap.put(student.getStudentId(), student);
            return new ResponseVo<>("学生修改成功", 200, 1);
        }
    
        /**
         * 输入studentId查询并返回对应的学生
         * @param studentId
         * @return
         */
        public ResponseVo getOneStudentByStudentId(Integer studentId){
            if(studentMap.containsKey(studentId) == false){
                return new ResponseVo<>("您所查询的学生不存在", 200, null);
            }
            return new ResponseVo<>("查询成功", 200, studentMap.get(studentId));
        }
    
        /**
         * 获取所有学生
         * @return
         */
        public ResponseVo> getAllStudent(){
            return new ResponseVo<>("获取全部学生成功", 200, studentMap.values());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    八、创建controller层

    package com.swagger.controller;
    
    import com.swagger.entity.Student;
    import com.swagger.service.StudentService;
    import com.swagger.vo.ResponseVo;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.Collection;
    
    
    /**
     * Controller
     */
    @Api("学生管理相关接口")
    @RestController //@Controller + @ResponseBody
    @RequestMapping("/student")
    public class StudentController {
        @Autowired
        private StudentService studentService;
    
        /**
         * 添加一名学生
         * @param student
         * @return
         */
        @ApiOperation("添加一名学生")// 为每个handler添加方法功能描述
        @PostMapping("/add_student.action")
        @ApiImplicitParam(name = "student", value = "所添加的学生", dataTypeClass = Student.class)
        public ResponseVo addOneStudent(Student student) {
            return studentService.addOneStudent(student);
        }
    
        /**
         * 根据studentId删除一名学生
         * @param studentId
         * @return
         */
        @ApiOperation("根据studentId删除一名学生")
        @DeleteMapping("/delete_student/{studentId}.action")
        public ResponseVo deleteOneStudentByStudentId(@PathVariable Integer studentId) {
            return studentService.deleteOneStudentByStudentId(studentId);
        }
    
        /**
         * 修改一名学生
         * @param student
         * @return
         */
        @ApiOperation("修改一名学生")
        @PutMapping("/update_student.action")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "studentId", value = "学号", required = true), //required为是否必填项
                @ApiImplicitParam(name = "studentName", value = "学生姓名", required = false),
                @ApiImplicitParam(name = "studentSex", value = "学生性别", required = false),
                @ApiImplicitParam(name = "studentScore", value = "学生分数", required = false)
        })
        public ResponseVo updateOneStudent(Student student) {
            return studentService.updateOneStudent(student);
        }
    
        /**
         * 根据id获取一名学生
         * @param studentId
         * @return
         */
        @ApiOperation("根据id获取一名学生")
        @GetMapping("/get_ont_student/{studentId}.action")
        public ResponseVo getOntStudentByStudentId(@PathVariable Integer studentId) {
            return studentService.getOneStudentByStudentId(studentId);
        }
    
        /**
         * 获取全部学生
         * @return
         */
        @ApiOperation("获取全部学生")
        @GetMapping("/get_all_student.action")
        public ResponseVo> getAllStudent() {
            return studentService.getAllStudent();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    九、重新启动项目,使用浏览器访问swagger的url进行测试

    在这里插入图片描述

    源码地址:https://gitee.com/wang-jian-fei/springboot-swagger-mongoDB.git

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    LeetCode-891. 子序列宽度之和【排序,数学,数组】
    02-文本属性
    Nanoprobes丨Nanogold®-抗体和链霉亲和素偶联物
    基于springboot养老院管理系统
    Windows中毒应急方式
    树论_1.
    基于双二阶广义积分器的三相锁相环(DSOGI-PLL)Simulink仿真
    ISO IEC 27001-2022 《信息安全、网络安全和隐私保护 信息安全管理系统 要求》
    .Net CLR
    异步爬取+多线程+redis构建一个运转丝滑且免费http-ip代理池 (三)
  • 原文地址:https://blog.csdn.net/m0_59092234/article/details/126063992