为什么 springfox-swagger2 UI 告诉我“无法推断基本 url". [英] Why does springfox-swagger2 UI tell me "Unable to infer base url."

查看:89
本文介绍了为什么 springfox-swagger2 UI 告诉我“无法推断基本 url".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 springfox-swagger2 UI 告诉我 Unable to infer base url. 据我所知,我使用的是典型的 Swagger spring-boot 配置.

正如您在屏幕截图中看到的,支持 UI 的 swagger-fox 网址是 example.com/api .注意:当我导航到:

我的配置看起来像这样(我已经尝试了这个配置的各种变体,从网上搜索建议):

@EnableSwagger2@EnableWebMvc@ComponentScan(basePackages = {"com.company.project"})公共类 SwaggerConfig{@豆角,扁豆公共 Docket api() {返回新的 Docket(DocumentationType.SWAGGER_2).选择().apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot"))).apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud"))).apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc"))).paths(PathSelectors.any()).建造();}}

<依赖><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></依赖><依赖><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></依赖>

注意:有趣的是,当我尝试 2.6.0 版时,我没有看到模态弹出窗口,但我的 Swagger UI 显示 0 api 内容.所以,我知道 modal 一定是相当新的吗?

如果这里没有足够的信息,请给我留言.

解决方案

原来我的问题的解决方案是将 @EnableSwagger2 注释移动到主 Configuration 类中,以及 Docket豆角,扁豆.

我在一个带有 Docket Bean 的子包中创建了一个单独的类,我期待它被 Spring 扫描并加载 bean.也许我没有用 @Component 注释那个 docket 类,或者可能是其他问题,但我让它工作了.

Why does springfox-swagger2 UI tell me Unable to infer base url. As far as I know, I am using a typical Swagger spring-boot configuration.

As you can see in the screenshot, the swagger-fox url backing the UI is example.com/api . NOTE: I get a standard Spring Whitelabel Error Page when I navigate to: https://localhost:9600/api/v2/api-docs/ . I suspect this is the root of the problem? I see no errors that Spring didn't load springfox-swagger2 and so I don't know why that isn't working.

My config looks something like this (and I have tried all sorts of variations of this config, from searching the net for advice):

@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = {"com.company.project"})
public class SwaggerConfig
{
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc")))
                .paths(PathSelectors.any())
                .build();
    }
}

And

<!-- to generate /swagger-ui.html -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>

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

NOTE: Interestingly, when I try version 2.6.0, I don't get the modal popup but my Swagger UI shows 0 api content. So, i know that modal must be fairly new?

If there is not enough info here, leave me a comment.

解决方案

It turns out that the solution to my problem was to move the @EnableSwagger2 annotation into the main Configuration class, along with the Docket Bean.

I had created a separate class in a sub-package with the Docket Bean and I was expecting it to get scanned by Spring and load the bean. Perhaps I failed to annotate that docket class with @Component, or maybe the the problem was something else, but I got it working.

这篇关于为什么 springfox-swagger2 UI 告诉我“无法推断基本 url".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆