想彻底掌握Swagger,这一篇就行了!

原创
小哥 3年前 (2022-11-07) 阅读数 31 #C#
文章标签 .net

Swagger

一、Swagger基础

swagger:
据说它是世界上最受欢迎的。Api框架
RestFul Api文档在线自动生成工具。=>Api文档与Api定义同步更新
直接运行,可以在线测试。Api接口
支持多种语言,java php

二、SpringBoot集成swagger

1,新建一个spring项目



2、修改maven仓库

3,导入依赖项



    io.springfox
    springfox-swagger2
    2.9.2




    io.springfox
    springfox-swagger-ui
    2.9.2

4,编写Hello工程

import org.springframework.web.bind.annotation.RestController;

/**
 * @author potential
 */
@RestController
public class HelloController {

    /**
     *  每个项目都有一个默认请求。 /error
     */
    @RequestMapping(value="/hello")
    public String hello(){
        return "hello";

    }


5、配置swagger

创建SwaggerConfig类,添加以下内容

package com.kuang.swagger.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 *SwaggerConfig 必须配置springboot里面
 * 所以你需要添加@Configuration注解
 * @author potential
 */
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {

}

6、测试

开始,主开始类,输入。 http://localhost:8080/swagger-ui.html ,查看swagger的web界面,显示下一页,表示成功集成。

三、配置Swagger信息

Swagger的bean实例Docket:
SwaggerConfig类 在中,添加以下内容以表示Swagger的bean实例Docket实例化

/**
     * 配置swagger的Docket的Bean实例
     */
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2);

3.1 配置Swagger信息的ApiInfo

1、在 SwaggerConfig类 在中,添加以下内容,set。ApiInfo信息,并将其放入实例化。Docket中调用

  /**
 *SwaggerConfig 必须配置springboot里面
 * 所以你需要添加@Configuration注解
 * @author potential
 */
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
    /**
     * 配置swagger的Docket的Bean实例
     */
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());

    }
    /**
     * 配置Swagger信息的apiInfo
     */
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("李","https://blog.csdn.net/wangzhihao1994/article/details/108408420","18058429987@qq.com");
        return  new ApiInfo(

                "狂神的Swagger日记",
                "即使是最小的帆也能航行很远。",
                "1.0",
                "https://blog.csdn.net/wangzhihao1994/article/details/108408420",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }


2,运行,查询结果

3.2 Swagger配置扫描界面

2、在 SwaggerConfig类 的方法docket中,添加如下内容,配置扫描界面,具体属性如下代码中的解释。

 /**
     * 配置swagger的Docket的Bean实例
     */
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                /*
                如何配置要扫描的接口
                basePackage:指定要扫描的包
                any():全部扫描
                none():无扫描
                withClassAnnotation:扫描类上的注释,参数是注释的反映。
                withMethodAnnotation:关于扫描方法的评论
                例如:.apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
                      仅扫描类RestController的这些类
                 */
                .apis(RequestHandlerSelectors.basePackage("com.kuang.swagger.controller"))
                //要筛选的路径 等同于仅扫描Kuang以下请求
                .paths(PathSelectors.ant("/kuang/**"))
                .build();

    }

3.3 配置Swagger是否启动

enable();控制swagger是否启动
若为false,无法启动 swagger的ui界面将提示
若为true,您可以开始

相关面试问题:
我只是想要我的swagger用于生产环境,在发布时不适用?
思路:1,确定生产环境 flag=false
2、注入enable(flag)

解答:
1,模拟多环境配置


其中 application.properties 添加以下内容:

#激活dev环境
spring.profiles.active=dev 

application-dev.properties 添加以下内容:

server.port=8081

application-pro.properties 添加以下内容:

server.port=8082

2、将 SwaggerConfig类 中的内容修改为以下内容

/**
 *SwaggerConfig 必须配置springboot里面
 * 所以你需要添加@Configuration注解
 * @author potential
 */
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
    /**
     * 配置swagger的Docket的Bean实例
     */
    @Bean
    public Docket docket(Environment environment){

        //设置为显示swagger环境
        Profiles profiles=Profiles.of("dev");
        /*
         获取项目环境。
         通过environment.acceptsProfiles()确定他们是否处于他们设定的环境中。
         */
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                .select()
                /*
                如何配置要扫描的接口
                basePackage:指定要扫描的包
                any():全部扫描
                none():无扫描
                withClassAnnotation:扫描类上的注释,参数是注释的反映。
                withMethodAnnotation:关于扫描方法的评论
                例如:.apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
                      仅扫描类RestController的这些类
                 */
                .apis(RequestHandlerSelectors.basePackage("com.kuang.swagger.controller"))
                //要筛选的路径 等同于仅扫描Kuang以下请求
                .paths(PathSelectors.ant("/kuang/**"))
                .build();

    }
    /**
     * 配置Swagger信息的apiInfo
     */
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("李","https://blog.csdn.net/wangzhihao1994/article/details/108408420","18058429987@qq.com");
        return  new ApiInfo(

                "狂神的Swagger日记",
                "即使是最小的帆也能航行很远。",
                "1.0",
                "https://blog.csdn.net/wangzhihao1994/article/details/108408420",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}


测试结果:

3.4 配置Api文件分组

在SwaggerConfig类的Docket类中添加以下内容:

.groupName("狂神")

相关题目
如何配置多个组

添加多个Docket实例即可
在SwaggerConfig类中,添加以下内容:

@Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("C");
    }


测试:

3.5 配置Api对文件的评论

在controller添加评论

测试

注意:
这里,在controller中添加的注释不应关联pojo添加到的评论
在pojo如果要添加注释,所使用的注释如下:

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除