代码之家  ›  专栏  ›  技术社区  ›  Pablo Fernandez

添加了Springfox招摇UI,但它不工作,我缺少什么?

  •  35
  • Pablo Fernandez  · 技术社区  · 7 年前

    http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

    compile "io.springfox:springfox-swagger2:2.7.0"
    compile "io.springfox:springfox-swagger-ui:2.7.0"
    

    并将SpringFox的招摇配置为:

    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 SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    

    但是,大摇大摆的用户界面似乎没有启用。我试过:

    我得到的只是:

    Whitelabel Error Page
    
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    
    Mon Sep 11 09:43:46 BST 2017
    There was an unexpected error (type=Method Not Allowed, status=405).
    Request method 'GET' not supported
    

    在日志上我看到:

    2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported
    2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
    

    http://localhost:8080/swagger-resources 返回:

    [{"name": "default",
      "location": "/v2/api-docs",
      "swaggerVersion": "2.0"}]
    

    我错过了什么?

    21 回复  |  直到 7 年前
        1
  •  63
  •   Ravi Parekh    3 年前

    Springfox 3.0.0仅适用于Spring Boot<=2.6.0-M2,但不包括以上版本

    Options for Spring Boot 2.6 M2 <
    1、弹簧。mvc。路径匹配。匹配策略=ANT\u PATH\u MATCHER#->应用程序。属性
    -但不带执行器

    3、迁移到 springdoc-openapi-ui -步骤与io相同。springfox>=3.X

    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-schema</artifactId>
    <version>2.9.2</version>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
    </dependency>
    浏览器URL
    http://localhost:8080/swagger-ui.html
    浏览器URL
    http://localhost:8080/swagger-ui/
    http://localhost:8080/swagger-ui/index.html
    必须需要

    mvn clean


    清理

    @Configuration
    @EnableSwagger2
    @配置
    @启用招摇2
        2
  •  32
  •   Muralidharan.rade    4 年前

    我尝试了这些答案中的大多数,最终的解决方案是缓慢的。。

    正确的URL如下

    http://localhost:8080/swagger-用户界面/

    请参阅完整的招摇设置: http://muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/

        3
  •  26
  •   viveknaskar    4 年前

    已经有很多答案说明了正确的答案,但仍然存在一些关于错误的混淆。

    <!-- Swagger dependency -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    

    一旦应用程序启动, 您可以点击任意一个新的swagger URL来获取文档 .

    http://localhost:8080/swagger-用户界面/

    选项2: http://localhost:8080/swagger-用户界面/索引。html

        4
  •  10
  •   StanislavL    7 年前

    引用自 https://github.com/springfox/springfox/issues/1672

    通过调查评论中的各种想法发现。

        5
  •  6
  •   Ousama    4 年前

    对于Spring版本>=2.2,您应该添加依赖项springfox boot starter

    pom。xml:

    <properties>
        <java.version>1.8</java.version>
        <io.springfox.version>3.0.0</io.springfox.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-data-rest</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${io.springfox.version}</version>
        </dependency>
    </dependencies>
    

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

    招摇UI链接: http://localhost:8080/swagger-用户界面/索引。html#/

        6
  •  4
  •   jlimadev    3 年前

    对于版本3.0.0,只有一个依赖项:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    

    之后,您可以在以下位置访问swagger ui:

    • http://localhost:8080/swagger-ui/#
    • http://localhost:8080/swagger-ui/index.html

    对于版本2。x、 x

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${io.springfox.version}</version>
    </dependency>
    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${io.springfox.version}</version>
    </dependency>
    

    http://localhost:8080/swagger-ui

        7
  •  3
  •   halfer    4 年前

    如果您使用的是Spring Boot版本>=2.2,我建议使用SpringFox Swagger版本3.0.0。保留您的pom。xml依赖项配置如下:

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
    </dependency>
    

    保持您的Swagger配置类如下:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
    public static final Contact DEFAULT_CONTACT = new Contact(
            "Sample App", "http://www.sample.com", "sample@gmail.com");
    
    public static final ApiInfo DEFAULT_API_INFO = new ApiInfo(
            "Awesome API Title", "Awesome API Description", "1.0",
            "urn:tos", DEFAULT_CONTACT,
            "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", Arrays.asList());
    
    private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES =
            new HashSet<String>(Arrays.asList("application/json",
                    "application/xml"));
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(DEFAULT_API_INFO)
                .produces(DEFAULT_PRODUCES_AND_CONSUMES)
                .consumes(DEFAULT_PRODUCES_AND_CONSUMES);
     }
    }
    

        8
  •  2
  •   fpezzini    5 年前

    我也遇到了这个问题,问题是我们有一个没有路径映射的控制器(因此映射到“/”。这阻止了对swagger ui资源的请求。

        9
  •  2
  •   Jakub Słowikowski    3 年前

    我试着把招摇结合起来 @Configuration @EnableWebMvc 仅有一个的

    不工作:

    @Configuration
    @EnableSwagger2
    @EnableWebMvc
    public class SwaggerConfiguration 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/**")
                    .addResourceLocations("classpath:/META-INF/resources/");
    
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
    

    解决方案

    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration {
    
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    
    
    @Configuration
    @EnableWebMvc
    public class WebAppConfig implements WebMvcConfigurer {
    
        @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/");
        }
    
    }
    
        10
  •  1
  •   WalkingKali    3 年前

    io。springfox>=3,并使用SpringSecurity

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    

    @Configuration
    @EnableSwagger2
    public class SpringFoxConfig implements WebMvcConfigurer {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(getApiInfo());
    }
    
    private ApiInfo getApiInfo() {
        return new ApiInfo(
                "company_name",
                "message here",
                "VERSION_1",
                "TERMS OF SERVICE URL",
                new Contact("company", "url", "EMAIL"),
                "LICENSE",
                "LICENSE URL",
                Collections.emptyList()
          );
         }
       }
    

    WebConfig类(确保 @EnableWebMvc 注释未使用,否则会出错)

       @Configuration
      //@EnableWebMvc
       public class WebConfig implements WebMvcConfigurer {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                    .allowedMethods("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", 
               "OPTIONS");
            }
          }
    

    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private static final String[] AUTH_WHITELIST = {
            // -- Swagger UI v2
            "/v2/api-docs",
            "/swagger-resources",
            "/swagger-resources/**",
            "/configuration/ui",
            "/configuration/**",
            "/configuration/security",
            "/swagger-ui.html",
            "/webjars/**",
            // -- Swagger UI v3 (OpenAPI)
            "/v3/api-docs/**",
            "/swagger-ui/**",
            "/swagger-ui/",
            "/swagger-ui"
            // other public endpoints of your API may be appended to this array
    
            @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception{
        httpSecurity.cors();
        httpSecurity.csrf().disable();
        httpSecurity.sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .authorizeRequests()
                    .antMatchers(AUTH_WHITELIST).permitAll()
                    .anyRequest()
                    .authenticated();
    
                    httpSecurity.addFilterBefore(jwtRequestFilter, 
                                 UsernamePasswordAuthenticationFilter.class);
             }
         };
    
        11
  •  1
  •   in3des    3 年前

    http://localhost:8080/swagger-ui/-不工作

    ...最后,问题是由我的pom中的一些混乱引起的。xml

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    

        12
  •  0
  •   Prasanth Rajendran    5 年前

    @RequestMapping("/") 控制器级(之后 @RestController\@Controller Request method 'GET' not supported Thanks to Dhermanns's suggestion

        13
  •  0
  •   Farzad Vertigo    5 年前

    添加 @RequestMapping("/") 在控制器级别,它起作用。

        14
  •  0
  •   iengchen    5 年前

    结论:我发现maven存储库下没有jar文件 ${user_home}/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2

    我通过以下步骤解决了这个问题:

    • 检查2.9.2下的文件是否完整如果缺少文件,请删除整个2.9.2目录
    • reimport all maven projects 在intellij idea中

        15
  •  0
  •   Aswani    5 年前

    我得到了招摇过市的问题/招摇过市的用户界面。不支持html请求方法“get”\不支持请求方法“get”。\支持的方法post

        16
  •  0
  •   Madhuka Dilhan    3 年前

    如果您使用 版本-V3 | | io。springfox>=3.0.0

    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-boot-starter</artifactId>
       <version>3.0.0</version>
    </dependency>
    

    Java代码

    @Configuration
    @EnableSwagger2
    
    public class SwaggerConfig {
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("Your Controller package name"))
                .paths(PathSelectors.any()).build();
    }
    

    V3浏览器URL-> http://localhost:8080/swagger-ui/#/

        17
  •  0
  •   M ONeill    3 年前

    在尝试了许多这些建议后,我仍然没有任何运气,我偶然发现了这篇博文: https://medium.com/swlh/openapi-swagger-ui-codegen-with-spring-boot-1afb1c0a570e

    作者说:“注意:如果你得到了白标签错误页面,试着重新启动IDE并再次运行项目。”

    这很有魅力。

        18
  •  0
  •   jakub-adamczewski    3 年前

    这个评论节省了我很多时间。简而言之,我发现我的项目中有人向控制器添加了这样的映射:

    @RestController("/api/test")
    

    当然,它应该是这样的:

    @RestController
    @RequestMapping("/api/test")
    

    @RestConroller的文档更准确地解释了这个问题:

    该值可能表示对逻辑组件名称的建议,以 返回:建议的组件名称(如果有)(或空字符串 否则)自:4.0.1

        19
  •  0
  •   Alex Osmolovsky    3 年前

    OAS_30 在摘要构造函数中:

    @Bean
    public Docket api() {
      return new Docket(DocumentationType.OAS_30)
                               .select()
                               .apis(RequestHandlerSelectors.any())
                               .paths(PathSelectors.any())
                               .build();
    }
    
        20
  •  0
  •   Aditya Rewari    3 年前

    对我来说,这是一个问题,因为现有的API

    Existing API 在控制器上

    http://localhost:8080/{PathParam}

    我把它改成了

    http://localhost:8080/domain/{PathParam}

        21
  •  0
  •   Suresh    3 年前
    Had to do 2 things to fix it:
    1) added below dependency and removed other swagger dependencies
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    2) org.springframework.security dependency in pom.xml was blocking the swagger-ui, so added below code to bypass security for swagger ui:
    @Configuration
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/v2/api-docs",
                                       "/swagger-resources/**",
                                       "/swagger-ui/**");
        }
    }