zoukankan      html  css  js  c++  java
  • swagger简单介绍及SpringBoot集成swagger

    一:简单介绍

      在前后端分离项目中,前后端唯一的联系就是接口,前端通过调用接口得到数据渲染页面,所以很多企业都要求有规范的文档。

      swagger是一个框架,可根据代码直接生成文档,无需程序员手动编写文档。

                                               并且支持在线测试,参数和格式都定义好了,在界面输入相应的值即可在线测试。

    二:使用

      1.导入相关依赖

    <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
    </dependency>
    <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
    </dependency>

      2.创建配置类

    import com.google.common.base.Predicates;
    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.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    import springfox.documentation.service.Contact;
    
    @Configuration
    @EnableSwagger2
    public class Swagger2Config {
    
        @Bean
        public Docket webApiConfig(){
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("webApi")
                    .apiInfo(webApiInfo())
                    .select()
                    //只显示api路径下的页面
                    .paths(Predicates.and(PathSelectors.regex("/api/.*")))
                    .build();
    
        }
    
        @Bean
        public Docket adminApiConfig(){
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("adminApi")
                    .apiInfo(adminApiInfo())
                    .select()
                    //只显示admin路径下的页面
                    .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                    .build();
    
        }
    
        private ApiInfo webApiInfo(){
    
            return new ApiInfoBuilder()
                    .title("网站-API文档")
                    .description("本文档描述了网站微服务接口定义")
                    .version("1.0")
                    .contact(new Contact("xxx", "http://xxx.com", "xxxxxxxx@qq.com"))
                    .build();
        }
    
        private ApiInfo adminApiInfo(){
    
            return new ApiInfoBuilder()
                    .title("后台管理系统-API文档")
                    .description("本文档描述了后台管理系统微服务接口定义")
                    .version("1.0")
                    .contact(new Contact("xxx", "http://xxx.com", "xxxxxxxxx@qq.com"))
                    .build();
        }
    
    
    }

      3.启动类配置注解扫描配置类所在的包:@ComponentScan("xxx")

  • 相关阅读:
    38) 收集centos所有版本镜像下载地址
    37) 查看linux 操作系统为多少位
    php面向对象高级应用一
    php form表单的验证+提交到数据库
    php获取form表单数据
    php form表单的提交
    php form表单概念
    php日期和时间的应用
    php日期和时间函数
    php字符串函数操作实例(2)
  • 原文地址:https://www.cnblogs.com/3w9898/p/15552135.html
Copyright © 2011-2022 走看看