java - Springfox 从 2.9.2 更新到 2.10.4 后错误 "Unable to infer base url. ..."

标签 java spring-boot swagger springfox

我刚刚将 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>
因类(class)变动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 时,我收到以下消息:

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:


不用说,相同的配置(除了上面提到的 2 个更改)适用于 2.9.2。前面问题中的大部分提示都是添加
@EnableSwagger2
但由于此注释在 2.10.X 中已更改为 @EnableSwagger2Mvc@EnableSwagger2Flux这无济于事。

最佳答案

来自 springfox issue tracker :

Oh please dont use 2.10... it was an intermediate step so that people who were on 3.0.0-SNAPSHOT can continue using a released version if needed.


我建议暂时恢复到 2.9.2。

关于java - Springfox 从 2.9.2 更新到 2.10.4 后错误 "Unable to infer base url. ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62573076/

相关文章:

java - log4j 在获取我的配置时显示错误

java - 无法解析符号 'default_web_client_id' 错误,但应用程序运行成功

java - java中如何避免重复的数据库连接?

java - 在 Spring 4 JPA Hibernate 5 中使用 @NamedStoredProcedureQuery 的过程模式

java - Swagger @ApiParam 注释使在 Swagger UI 中不需要使用 @PathVariable 注释的参数

java - JUnitCore 在 Eclipse 插件中给出 "No runnable methods"错误

java - 如何设置在spring boot中使用的redis数据库编号

java - 如何生成与 java 联合的 graphql 模式?

amazon-web-services - 通过 terraform 部署的 AWS API Gateway 和 Lambda 函数 -- 由于配置错误 : Invalid permissions on Lambda function 导致执行失败

spring-mvc - Spring Swagger UI : what is difference between io. swagger、io.springfox 和 com.mangofactory