我刚刚将 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
这无济于事。
最佳答案
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/