【问题标题】:After Update of Springfox from 2.9.2 to 2.10.4 error "Unable to infer base url. ..."将 Springfox 从 2.9.2 更新到 2.10.4 后出现错误“无法推断基本 URL。...”
【发布时间】:2020-10-15 18:38:15
【问题描述】:

我刚刚将我的 Spring Boot 应用程序(版本 2.3.1.RELEASE)中的 Springfox 依赖项从 2.9.2 更新到了 2.10.4。

<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
<swagger.version>2.10.4</swagger.version>

由于springfox.documentation.* 包中的类更改,我不得不更改我的配置类中的注释

@EnableSwagger2

@EnableSwagger2WebMvc

谓词导入也从 Google Guave 更改为 java.util.function。我当前的配置类是这样的

package de.rewe.zlip.config;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).globalOperationParameters(globalOperationParameters())
                                                      .select()
                                                      .apis(sourceScannedForRestApis())
                                                      .paths(PathSelectors.any())
                                                      .build()
                                                      .apiInfo(apiEndPointsInfo())
                                                      .genericModelSubstitutes(Optional.class);
    }

    private List<Parameter> globalOperationParameters() {
        List<Parameter> operationParameters = new LinkedList<>();
        Parameter authorizationParameter = new ParameterBuilder().name("Authorization")
                                                                 .description("Your Bearer token ")
                                                                 .modelRef(new ModelRef("string"))
                                                                 .parameterType("header")
                                                                 .build();
        operationParameters.add(authorizationParameter);
        return operationParameters;
    }

    private Predicate<RequestHandler> sourceScannedForRestApis() {
        return RequestHandlerSelectors.basePackage("de.my.package");
    }

    private ApiInfo apiEndPointsInfo() {
        return new ApiInfoBuilder().title("TEST SERVER REST API")
                                   .description("REST API provided for the TEST web application")
                                   .contact(contactInfo())
                                   .version("v1.0")
                                   .build();
    }

    private Contact contactInfo() {
        return new Contact("Test Team", "https://", "test@test.com");
    }

}

当我现在打开 http://localhost:8080/swagger-ui.html 时,我收到以下消息:

无法推断基本网址。这在使用动态 servlet 时很常见 注册或当 API 位于 API 网关后面时。基本网址是 提供所有 swagger 资源的根。例如如果 该 API 可在http://example.org/api/v2/api-docs 获得,然后 基本网址是http://example.org/api/。请输入位置 手动:

不用说,相同的配置(除了上面提到的 2 个更改)适用于 2.9.2。前面问题中的大部分tips都在添加

@EnableSwagger2

但由于此注释在 2.10.X 中已更改为 @EnableSwagger2Mvc@EnableSwagger2Flux,这将无济于事。

【问题讨论】:

    标签: java spring-boot swagger springfox


    【解决方案1】:

    来自springfox issue tracker

    哦,请不要使用 2.10...这是一个中间步骤,以便使用 3.0.0-SNAPSHOT 的人可以在需要时继续使用已发布的版本。

    我建议暂时恢复到 2.9.2。

    【讨论】:

      【解决方案2】:

      你可以在下面试试。

      1. 将此依赖项添加到您的 pom.xml 中
      <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>${your.spring.fox.version}</version>
      </dependency>
      
      1. 将此行添加到您的主类或 swagger 配置类中
      @Import(SpringDataRestConfiguration.class)
      

      【讨论】:

      • 与问题无关。
      猜你喜欢
      • 2019-10-10
      • 1970-01-01
      • 2019-11-15
      • 2020-06-02
      • 2018-08-15
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多