swagger只需一篇

本文介绍了如何在Spring Boot项目中使用Springfox Swagger2进行API文档的配置,重点讲解了如何根据环境变量动态决定是否显示Swagger,并演示了如何为不同分组设置API文档。
摘要由CSDN通过智能技术生成
package com.atqh.qhcommunity.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.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;

import java.util.ArrayList;

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {

    //配置多个分组
/*
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
    }
*/

    @Bean
    public Docket docket(Environment environment) {

        // 设置要显示swagger的环境
        Profiles of = Profiles.of("dev", "test");
        // 判断当前是否处于该环境
        // 通过 enable() 接收此参数判断是否要显示
        boolean b = environment.acceptsProfiles(of);


        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("azhe")
                .apiInfo(apiInfo())
                .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.atqh.qhcommunity.controller"))
                .paths(PathSelectors.any())//配置如何通过path过滤
                /*any() // 任何请求都扫描
                 none() // 任何请求都不扫描
                 regex(final String pathRegex) // 通过正则表达式控制
                 ant(final String antPattern) // 通过ant()控制**/
                .build()
                .enable(true)//配置是否启用Swagger,如果是false,在浏览器将无法访问
                /*.enable(b);*/
                ;
    }

    private ApiInfo apiInfo() {
//配置文档信息
        Contact contact = new Contact("阿哲", "2033244875@qq.com", "2033244875@qq.com");
        return new ApiInfo(
                "qhCommunity后端接口文档Swagger", // 标题
                "qhCommunity后端接口", // 描述
                "v1.0", // 版本
                "2033244875@qq.com", // 组织链接
                contact, // 联系人信息
                "Apach 2.0 许可", // 许可
                "许可链接", // 许可连接
                new ArrayList<>()// 扩展
        );

    }


}

pom.xml 

 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- 引入swagger-ui-layer包 /document.html-->
        访问 http://localhost:8888/document.html
        <dependency>
            <groupId>com.zyplayer</groupId>
            <artifactId>swagger-mg-ui</artifactId>
            <version>1.0.6</version>
        </dependency>

注意!!!

springboot项目版本过高会与swagger不兼容,应手动降低springboot版本,例如采用2.5.6可以解决问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值