代码之家  ›  专栏  ›  技术社区  ›  riorio

使用Spring Boot 2.0的狂妄自大会导致404错误页

  •  6
  • riorio  · 技术社区  · 7 年前

    我正在尝试集成我的spring boot版本 2.0.1.RELEASE 具有 Swagger .

    从这个 blog post 似乎只需添加两个maven依赖项就很容易了,一切都应该正常。

    因此,我向pom添加了以下依赖项:

            <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
    

    创造了 SwaggerConfig 豆类:

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    @Bean
    public Docket api() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    
        return docket;
       }
    }
    

    在properties文件中,我在尝试使其工作的过程中得到了这3个条目:

    spring.application.name=cat-service
    management.server.servlet.context-path=/cat-service
    server.servlet.contextPath=/cat-service
    

    但最后,当访问

    http://localhost:8080/cat-service/api/v2/api-docs

    或位于的用户界面页

    http://localhost:8080/cat-service/swagger-ui.html

    我得到一个 page not found 错误。

    我发现 this issues in the swagger github page this question in stackoverflow 但我不能改变我的 404 错误。

    5 回复  |  直到 7 年前
        1
  •  9
  •   riorio    6 年前

    我可以让它与Spring Boot版本一起工作 2.0.4.RELEASE this blog post :

    我添加了这些依赖项:

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

    以及此配置文件:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SpringFoxConfig {
        @Bean
        public Docket apiDocket() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    

    而且成功了。

    可以通过/swagger ui.html访问swagger ui#

        2
  •  0
  •   Camilo    7 年前

    另一种可能是swagger配置文件的位置;您需要将其放在spring引导文件的同一个包或子包中。 如上图所示:

    Swagger config and Spring Boot file hierarchy

        3
  •  0
  •   Exel Staderlin    6 年前

    首先像这样在springboot文件的同一个包中添加swaggerconfig.java文件。

    @Configuration
    @EnableSwagger2
    @EnableWebMvc
    public class SwaggerConfig extends WebMvcConfigurerAdapter {                                    
        @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/");
       }
    
    }
    

    试试这个 { http://localhost:8080/spring-security-rest/api/swagger-ui.html } 或 { http://localhost:8080/spring-security-rest/swagger-ui.html }

    如果这不起作用,那么让我们尝试更改application.properties中的路径

    把这个添加到你的文件中

    server.servlet-path=/my-service
    

    试试这个 http://localhost:8080/my-service/swagger-ui.html (用户界面文档)

    http://localhost:8080/loop-service/v2/api-docs (JSON文档)

    结果: enter image description here

        4
  •  -2
  •   Illary Huaylupo    7 年前

    这对我有效,我使用了webmvcconfigurer而不是webmvcconfigureradapter,因为该类已经被弃用。

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig implements WebMvcConfigurer {
      @Bean
      public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()                
                .apis(RequestHandlerSelectors.basePackage("com.illary.controller"))
                .paths(PathSelectors.any())
                .build()            
                .apiInfo(metaData());
      }
      private ApiInfo metaData() {
        return new ApiInfoBuilder()
                .title("Spring Boot Swagger App")
                .description("\"Spring Boot Swagger Server App\"")
                .version("1.0.0")
                .license("Apache License Version 2.0")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
                .build();
      }
    
      public ApiInfo apiInfo() {
        final ApiInfoBuilder builder = new ApiInfoBuilder();
        builder.title("Swagger Test App").version("1.0").license("(C) Copyright Test")
        .description("The API provides a platform to query build test swagger api");
    
        return builder.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/");
      }
    }
    
        5
  •  -4
  •   gstackoverflow    7 年前

    搬走后对我起作用了 @EnableWebMvc