怎么配置swagger?

導入依賴

       <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--用于訪問http://localhost:8080/swagger-ui.html-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--用于訪問http://localhost:8080/doc.html-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

<!--因為swagger2 是依賴于Google的guava,我本項目中引入的swagger2是最高版本,因此引入下面的jar包,否側報錯“Failed to start bean 'documentationPluginsBootstrapper-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>

編寫swagger的配置文件

@Configuration
@EnableSwagger2
public class SwaggerConf {
    @Bean
    public Docket petApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //指定從那個包下面開始掃描注解
                .apis(RequestHandlerSelectors.basePackage("com.demo"))
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文檔")
                .version("1.0.0")
                .termsOfServiceUrl("localhost:8080/swagger-ui.html")
                .description("測試")
                .build();
    }
}

訪問localhost:8080/swagger-ui.html

遇到問題怎么辦?

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

原因
這是因為Springfox使用的路徑匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher

解決方法
修改application.yaml

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容