美文网首页Java
Swagger依赖和配置类

Swagger依赖和配置类

作者: 花伤情犹在 | 来源:发表于2022-05-05 13:38 被阅读0次

SpringBoot版本不要太高,2.6.0之前

<!--    swagger相关   -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

配置

#允许访问 接口开发文档
swagger:
  enable: true

swagger配置类:

@EnableSwagger2
@Configuration
public class Swagger2Config {

    @Value("${swagger.enable}")
    private boolean swaggerEnable;
    //是否允许显示swagger。此值可在application.yml中设定。
    //作为开关,可在生产环境和开发环境打开或关闭,简便易行。

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerEnable)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.cdwm.mrp.controller")) 指定扫描包
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                 //只显示api路径下的页面
                //.paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("客户记录API文档")
                .description("客户记录API文档")
                .version("1.0.0")
                .build();
    }
}

相关文章

网友评论

    本文标题:Swagger依赖和配置类

    本文链接:https://www.haomeiwen.com/subject/pngnyrtx.html