SpringBoot集成Swagger

SpringBoot集成Swagger

Swagger是啥?

Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务(废话);更多时候,你可以把它当成一个在线API文档!(实话)

集成依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.1</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

最简配置

在SpringBoot启动类上加注解即可:@EnableSwagger2

访问

http://ip:端口/swagger-ui.html

环境保护

在开发环境开启SwaggerUI ,生产环境关闭SwaggerUI 是因为开发环境是内部人员,生产环境是客户。为了程序的安全性需要关闭SwagggerUI

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment){
        //设置要显示的Swagger 环境
        Profiles profiles =Profiles.of("test");
        /**
         * 通过 environment.acceptsProfiles 返回的boolean值判断是否处在自己所设定的环境中
         */
        boolean flag = environment.acceptsProfiles(profiles);
        return  new Docket(DocumentationType.SWAGGER_2)
                .enable(flag) //enable配置是否自动启动swagger 如果为False则为不启动,浏览器中不能访问Swagger
                .select()
                .build();//构建者模式
    }
}

现在再次访问:
在这里插入图片描述

指定扫描

有些API接口不希望也没必要让外部知道时,我们不需要进行扫描,而只需要扫描自己希望暴露给外部的接口

@Bean
public Docket docket(Environment environment){
    //设置要显示的Swagger 环境
    Profiles profiles =Profiles.of("local");
    /**
     * 通过 environment.acceptsProfiles 返回的boolean值判断是否处在自己所设定的环境中
     */
    boolean flag = environment.acceptsProfiles(profiles);
    return  new Docket(DocumentationType.SWAGGER_2)
            .enable(flag) //enable配置是否自动启动swagger 如果为False则为不启动,浏览器中不能访问Swagger
            .select()
            //只扫描如下两个包目录下,并且以/warning/qosWarningMessage或者/sys/admin开头的接口
            .apis(Predicates.or(RequestHandlerSelectors.basePackage("com.paratera.console.biz.controller.warning"),
                    RequestHandlerSelectors.basePackage("com.paratera.console.biz.controller.system")))
            .paths(Predicates.or(PathSelectors.ant("/sys/admin/**"),PathSelectors.ant("/warning/qosWarningMessage/**")))
            .build();//构建者模式
}

在这里插入图片描述

Swagger个性化信息

	@Bean
    public Docket docket(Environment environment) {
        //设置要显示的Swagger 环境
        Profiles profiles = Profiles.of("local");
        /**
         * 通过 environment.acceptsProfiles 返回的boolean值判断是否处在自己所设定的环境中
         */
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(flag) //enable配置是否自动启动swagger 如果为False则为不启动,浏览器中不能访问Swagger
                .apiInfo(apiInfo())
                .select()
                //只扫描如下两个包目录下,并且以/warning/qosWarningMessage或者/sys/admin开头的接口
                .apis(Predicates.or(RequestHandlerSelectors.basePackage("com.paratera.console.biz.controller.warning"),
                        RequestHandlerSelectors.basePackage("com.paratera.console.biz.controller.system")))
                .paths(Predicates.or(PathSelectors.ant("/sys/admin/**"), PathSelectors.ant("/warning/qosWarningMessage/**")))
                .build();//构建者模式
    }

    /**
     * 配置Swagger个性化信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        //配置作者信息
        Contact DEFAULT_CONTACT = new Contact("老胡", "https://blog.csdn.net/huxiang19851114", "185888580@qq.com");
        return new ApiInfo(
                "老胡的Swagger API文档",
                "无敌总是那么寂寞",
                "v1.0",
                "https://blog.csdn.net/huxiang19851114",
                DEFAULT_CONTACT,
                "Swagger 2.0",
                "https://swagger.io/",
                new ArrayList());

    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值