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

时间:2020-06-30 08:56:29

标签: java spring-mvc spring-security swagger

我还没有Spring Boot,只有Spring MVC和Security。 我的配置:

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

http://localhost:8080/api/v2/api-docs上,我得到了巨大的JSON,但是在http://localhost:8080/api/swagger-ui.html上尝试获取ui时,我却找不到tomcat 404

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 Boot或添加注释@EnableSwagger2来解决该问题的可能方法,但对我而言这没用。

2 个答案:

答案 0 :(得分:0)

尝试一下:

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

或在您要访问文档时删除/api/

答案 1 :(得分:0)

我认为您需要在控制器所在的位置添加该软件包,以便通过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();  
     }