加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

碰一碰:Swagger3就是比2简朴粗暴

发布时间:2021-05-28 15:52:02 所属栏目:编程 来源:互联网
导读:Swagger3集成 Swagger今朝最新版本是3.0.0,在Spring Boot应用中集成Swagger3比老的Swagger2简朴多了,它提供了一个Starter组件。 dependency groupIdio.springfo
副问题[/!--empirenews.page--]

Swagger今朝最新版本是3.0.0,在Spring Boot应用中集成Swagger3比老的Swagger2简朴多了,它提供了一个Starter组件。

<dependency> 

    <groupId>io.springfox</groupId> 

    <artifactId>springfox-boot-starter</artifactId> 

    <version>3.0.0</version> 

</dependency> 

就这就可以了,简朴不?

至于有的教程说还要开启注解@EnableOpenApi,完全不必要。由于在springfox-boot-starter-3.0.0.jar下你可以找到一个spring.factories,认识Spring Boot的同窗都知道这个是一个Spring Boot 特有的SPI文件,可以或许自动的发明并注册Starter组件的设置。内里有这样的设置:

# Auto Configure 

org.springframework.boot.autoconfigure.EnableAutoConfiguration= 

springfox.boot.starter.autoconfigure.OpenApiAutoConfiguration 

顺藤摸瓜,找到总的设置类OpenApiAutoConfiguration:

@Configuration 

@EnableConfigurationProperties(SpringfoxConfigurationProperties.class) 

@ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true) 

@Import({ 

    OpenApiDocumentationConfiguration.class, 

    SpringDataRestConfiguration.class, 

    BeanValidatorPluginsConfiguration.class, 

    Swagger2DocumentationConfiguration.class, 

    SwaggerUiWebFluxConfiguration.class, 

    SwaggerUiWebMvcConfiguration.class 

}) 

@AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class, 

    HttpMessageConvertersAutoConfiguration.class, RepositoryRestMvcAutoConfiguration.class }) 

public class OpenApiAutoConfiguration { 

 

一些发明

我们找到了要害的一个处所@ConditionalOnProperty注解声明白当springfox.documentation.enabled为true时启用设置,并且默认值就是true。这很是有效,Swagger仅仅提议在开拓阶段行使,这个正好是个开关。其它偶然辰我们自界说设置的时辰最好把这个开关也加上:

// 自界说swagger3文档信息 

@Configuration 

@ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true) 

public class Swagger3Config { 

    @Bean 

    public Docket createRestApi() { 

        return new Docket(DocumentationType.OAS_30) 

                .apiInfo(apiInfo()) 

                .select() 

                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 

                .paths(PathSelectors.any()) 

                .build(); 

    } 

 

    private ApiInfo apiInfo() { 

        return new ApiInfoBuilder() 

                .title("Swagger3接口文档") 

                .description("更多请咨询felord.cn") 

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读