繁体   English   中英

尝试打开 swagger-ui.html 时出现 404

[英]I get 404, when trying to open swagger-ui.html

我没有 Spring 引导,只有 Spring MVC 和安全性。 我的配置:

@Configuration
@EnableTransactionManagement
@EnableWebMvc
@EnableSwagger2
@PropertySource("classpath:access.properties")
@ComponentScan(basePackages = {"com.company.service", "com.company.dao", "com.company.controller"})

public class SpringConfig extends WebMvcConfigurationSupport {

@Value("${url}")
private String URL;
@Value("${user}")
private String USER;
@Value("${password}")
private String PASSWORD;

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();
}

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

........ 

pom.xml

    <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>

On http://localhost:8080/api/v2/api-docs i get enormous JSON, but whe trying to get ui on http://localhost:8080/api/swagger-ui.html i get tomcat 404 not found

Tomcat Log

0:0:0:0:0:0:0:1 - - [30/Jun/2020:11:50:09 +0300] "GET /api/v2/api-docs HTTP/1.1" 200 20832
0:0:0:0:0:0:0:1 - - [30/Jun/2020:11:51:51 +0300] "GET /api/swagger-ui.html HTTP/1.1" 404 702
0:0:0:0:0:0:0:1 - - [30/Jun/2020:11:51:54 +0300] "GET /api/swagger-ui.html HTTP/1.1" 404 702

许多文章都讨论了使用 Spring 引导或添加注释@EnableSwagger2的问题的可能解决方案,但在我的情况下它没有用。

尝试这个:

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

或者当你想访问你的文档时删除/api/

我认为您需要在 controller 所在的位置添加 package,以便 swagger: 对其进行扫描。 apis(RequestHandlerSelectors.basePackage("your package"))

这是一个例子

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
     public Docket getDocketInstance() {
         return new Docket(DocumentationType.SWAGGER_2) 
                 .apiInfo(new ApiInfoBuilder()
                            .title("Spring Boot project")
                            .description("Spring Boot bootstrap project")
                            .version("0.1")
                            .license("Unlicense")
                            .build())
                  .select()  
                  .apis(RequestHandlerSelectors.basePackage("your package"))             
                  .paths(PathSelectors.any())                          
                  .build();  
     }
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.6</version>
</dependency>

对于 SPRING 引导版本 > 2.5。 使用此依赖项进行招摇。 它对我有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM