swagger集成遇到的坑一個


SpringBoot項目集成swagger項目遇到一個問題:

訪問swagger-ui.html

沒有加載到數據,也沒有加載到頁面的html和css資源

除了

1、添加swagger的pom依賴

2、swagger的配置文件

配置swagger的顯示內容

例如

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //為當前包路徑
                .apis(RequestHandlerSelectors.basePackage("com.XXXXX.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //構建 api文檔的詳細信息函數,注意這里的注解引用的是哪個
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //頁面標題
                .title("XXXX")
                //創建人
                .contact(new Contact("mall-screen", "https://XXXX.com.cn", ""))
                //版本號
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

  

還需要

3、有一個web的配置文件

例如:

@Configuration
public class CustomWebAppConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

  這樣訪問swagger-ui.html的時候,才可以正常訪問swagger-ui的頁面

這次是忘記配置web,導致。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM