Swagger使用记录

第一步Maven依赖导入
        <!-- swagger2 -->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- 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>
第二步启用@EnableSwagger2

注意:使用@EnableSwagger2注解开启Swagger功能, 然后用浏览器访问http://localhost:8080/swagger-ui.html ,若能打开该页面,则开启成功。

@SpringBootApplication
@EnableSwagger2
public class OmallApplication {
    public static void main(String[] args) {
        SpringApplication.run(OmallApplication.class, args);
    }
}
定制SwaggerConfig配置文件

创建SwaggerConfig配置文件,再创建一个Docket()对象并注入Spring容器中,通过设置Docket属性来配置Swagger

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    public ApiInfo apiInfo() {
        return new ApiInfo("omall的API文档标题",
                "omallAPI的描述信息",
                "1.0版本",
                "oleolema公司",
                new Contact("oleolema姓名", "https://www.github.com/oleolema网站", "oleolema@qq.com邮箱"),
                "Apache 2.0开源名称",
                "http://www.apache.org/licenses/LICENSE-2.0开源许可地址",
                new ArrayList());
    }

页面显示结果是这样的
在这里插入图片描述

配置需要扫描的接口
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                /*
                    RequestHandlerSelectors:配置扫描接口的方式
                        basePackage(String): 扫描指定包
                        any(): 扫描全部
                        none(): 不扫描
                        withClassAnnotation(Class): 扫描类上的注解
                        withMethodAnnotation(Class): 扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.yqh.omall.controller"))
                /*
                    PathSelectors : 过滤的路径
                        ant(String) : 正则路径
                 */
//                .paths(PathSelectors.any())
                .build();
    }
如果只希望Swagger在开发环境使用

在配置文件中激活开发模式

spring.profiles.active=dev

获取环境并设置enable

    @Bean
    public Docket docket(Environment environment) {
    	//获取当前激活环境
        boolean dev = environment.acceptsProfiles(Profiles.of("dev"));
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //只在开发模式启用
                .enable(dev)
      
    }
分组配置

只需要配置多个docket, 并设置不同的groupName名称

    @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组");
    }

在网页显示结果是这样的
在这里插入图片描述

全部配置
@Configuration
public class SwaggerConfig {
    
    @Bean
    public Docket docket(Environment environment) {
        boolean dev = environment.acceptsProfiles(Profiles.of("dev"));
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //只在开发模式启用
                .enable(dev)
                .select()
                /*
                    RequestHandlerSelectors:配置扫描接口的方式
                        basePackage(String): 扫描指定包
                        any(): 扫描全部
                        none(): 不扫描
                        withClassAnnotation(Class): 扫描类上的注解
                        withMethodAnnotation(Class): 扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.yqh.omall.controller"))
                /*
                    PathSelectors : 过滤的路径
                        ant(String) : 正则路径
                 */
//                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfo("omall的API文档",
                "omallAPI的描述信息",
                "1.0",
                "oleolema",
                new Contact("oleolema", "https://www.github.com/oleolema", "oleolema@qq.com"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值