swagger

本文档展示了如何在Spring Boot应用中配置Swagger2,以生成RESTful API的在线文档。通过`@EnableSwagger2`注解开启Swagger支持,并创建`SwaggerConfig`类配置Docket bean,设置API信息、主机地址以及扫描接口包路径。同时,提供了如何通过环境变量控制Swagger的启用状态。
摘要由CSDN通过智能技术生成
package com.leyou.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author 98050
 */
@Configuration
@EnableSwagger2 //开启 swagger
public class SwaggerConfig {
    @Bean
    public Docket api(Environment environment) {
      // 设置要显示swagger的环境
  // Profiles of = Profiles.of("dev", "test");
   // 判断当前是否处于该环境
   // 通过 enable() 接收此参数判断是否要显示
  // boolean b = environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2)
                .host("http://localhost:8081")
                .apiInfo(apiInfo())
                .enable(false) //配置是否启用Swagger,如果是false,在浏览器将无法访问
                .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                //指定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.leyou.controller"))
                // 配置如何通过path过滤 paths(PathSelectors.ant("/shashen/**"))扫描shashen下的全部方法
                // any()扫描所有,项目中的所有接口都会被扫描到
                //none() 不扫描接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("商城订单系统") // 头部信息
                .description("杀神") //可以写作者
                .version("1.0") //自定义版本
                .termsOfServiceUrl("https://gitee.com/") //想要显示的网址
                .build();
    }
}

```java
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值