zoukankan      html  css  js  c++  java
  • 初学SpringBoot集成Swagger的使用

    SpringBoot 集成Swagger

    1.新建一个SpringBoot 项目

    2.导入依赖

    有两个依赖

    Swagger2

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    
    

    Swagger ui

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    
    

    3.配置Swagger ==》config

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    }
    

    4.测试运行

    项目跑起来后,即可去浏览器键入下面的地址,就可以看到Swagger的接口管理页面
    http://localhost:8080/swagger-ui.html

    SwaggerConfig配置文件内容

    @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors()选择扫描方式
                //加了ApiOperation注解的类,才生成接口文档
                .apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class))
                //扫描指定包下的类,才生成接口文档
                .apis(RequestHandlerSelectors.basePackage("cn.cnkimall.modules.sys.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(security());
        }
    
    
        private ApiInfo apiInfo(String title, String version, String description) {
            return new ApiInfoBuilder()
                    .title(title)                 //标题
                    .description(description)     //说明
                    .version(version)             //版本号
                    .build();
        }
    
  • 相关阅读:
    JVM运行时数据区及对象在内存中初始化的过程
    一文搞懂List 、List<Object>、List<?>的区别以及<? extends T>与<? super T>的区别
    Java中创建泛型数组
    JavaBean详解
    Java常用命令及参数
    一文彻底搞懂Java中的环境变量
    类型信息
    java中的数组
    URL与URI的区别
    上行速率和下行速率
  • 原文地址:https://www.cnblogs.com/zzzqi/p/13082571.html
Copyright © 2011-2022 走看看