• Swagger总结



    Swagger总结

    • jar包 pom.xml
    • SpringBoot配置文件 application.proerties
    • Swagger配置文件 config
    • 控制层
    • Swagger注解

    1、创建SpringBoot项目Web

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    2、依赖配置pom.xml

    
    <dependency>
         <groupId>io.springfoxgroupId>
         <artifactId>springfox-boot-starterartifactId>
         <version>3.0.0version>
     dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、配置与刷新Maven

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    4、SpringBoot配置application.proerties

    # SpringBoot2.6.x以上需要配置,springfox3.0.x以上需要配置
    spring.mvc.pathmatch.matching-strategy=ant_path_matcher
    
    • 1
    • 2

    5、配置类config

    package com.sgz.swagger.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    import org.springframework.core.env.Profiles;
    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;
    
    /**
     * 日期:2022/8/15 - 15:54
     * 需求:配置Swagger
     */
    @Configuration  // 标识是一个配置页面
    @EnableSwagger2 // 标识开启Swagger,会扫描当前所在包以及子包中所有的注解
    public class SwaggerConfig {
    
    
        /**
         * 创建Docket类型的对象,并使用Spring容器bean管理,Docket是Swagger中的全局配置对象
         * @return
         */
        @Bean
        public Docket docket(Environment environment) {
            // API帮助文档的描述信息
            ApiInfo apiInfo = new ApiInfoBuilder()
                    .contact(new Contact("我", "https://www.baidu.com", "xxxxxx@qq.com"))  // 作者信息
                    .title("我的Swagger API开发文档")    // 文档标题
                    .description("我的文档描述")    // 文档描述
                    .version("1.1")         // 版本号
                    .build();   // 构建
    
            // 创建Docket类型的对象
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo)     // API帮助文档的描述信息
                    .groupName("我")   // 配置分组
                    .select()   // 获取Docket中的选择器
                    .apis(RequestHandlerSelectors.basePackage("com.sgz.swagger.controller"))    // 设置扫描包以及子包中的注解
                    .paths(PathSelectors.regex("/swagger/.*"))  // 过滤路径,使用正则表达式约束生成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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    6、pojo类

    package com.sgz.swagger.pojo;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    
    /**
     * 日期:2022/8/15 - 17:40
     * 需求:
     */
    
    @ApiModel("用户实体类")  // 描述实体类信息
    public class User {
    
        @ApiModelProperty("用户名")    // 描述实体类属性名信息
        private String username;
        @ApiModelProperty("密码")
        private String password;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    7、控制层controller

    package com.sgz.swagger.controller;
    
    import com.sgz.swagger.pojo.User;
    import io.swagger.annotations.*;
    import org.springframework.web.bind.annotation.*;
    import springfox.documentation.annotations.ApiIgnore;
    
    /**
     * 日期:2022/8/15 - 16:29
     * 需求:
     */
    @RestController
    @RequestMapping("/swagger")
    @Api(description = "增删改查API")   // 描述类信息
    public class MyController {
    
        @GetMapping("/get")
        @ApiOperation("用来获取用户名")    // 描述方法信息
        public String get(@ApiParam("用户名") String username) {   // 描述参数信息
            return "用户名:" + username;
        }
    
        @PostMapping("/post")
        @ApiOperation("用来提交用户信息")
        public User post(User user) {
            return user;
        }
    
        @PutMapping("/put")
        @ApiOperation("修改用户信息")
        @ApiImplicitParams(value = {
                @ApiImplicitParam(name = "username", value = "用户名"),
                @ApiImplicitParam(name = "password", value = "密码")})    // 描述多个参数信息
        public String put(String username, String password) {
            return "用户名:"+username+",密码" +password+"修改成功";
        }
    
        @DeleteMapping("/delete")
        @ApiOperation("删除用户")
        @ApiImplicitParam(name = "id", value = "id号")   // 描述参数信息
        public String delete(Integer id) {
            return id + "删除成功";
        }
    
        @ApiIgnore  // 忽略该方法,也可以忽略类
        @GetMapping("/h")
        public String hl(){
            return "被忽略的";
        }
    }
    
    
    • 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

    8、启动引导类

    package com.sgz.swagger;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    public class SwaggerMouldApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SwaggerMouldApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    9、成功

    在这里插入图片描述

  • 相关阅读:
    防止SQL注入攻击的综合解决方案
    高教社杯数模竞赛特辑论文篇-2023年C题:基于历史数据的蔬菜类商品定价与补货决策模型(附获奖论文及R语言和Python代码实现)
    将项目制作成Dcoker镜像并运行--DevOps学习第三章
    顺序表和链表
    XAMPP的MySQL配置
    vsphere 中 win虚拟机创建
    面向对象设计模式之访问者模式
    1亿条数据批量插入 MySQL,哪种方式最快?
    网络安全漏洞管理十大度量指标
    导入10G数据的SQL脚本的MySQL5.7中
  • 原文地址:https://blog.csdn.net/s17856147699/article/details/126335178