【发布时间】:2021-04-06 05:00:46
【问题描述】:
我使用 Springboot 和 swagger 3:
<!-- SWAGGER -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
我对所有端点使用默认的 /api 前缀。
这就是我配置 SwaggerConfig 的方式:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static final String AUTHORIZATION_HEADER = "Authorization";
@Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(apiKey()))
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build().pathMapping("/api");
return docket;
}
private ApiKey apiKey() {
return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
}
// ......
}
当我尝试访问我的 swagger-ui http://myhost/swagger-ui 时,我会弹出一条消息 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: ,要求我定义位置。
当我手动输入前缀:http://myhost/api 时,一切都很好。
知道如何定义我的 REST API 前缀吗?
【问题讨论】:
-
嗨,我遇到了同样的问题,stackoverflow.com/a/53437951/924036 上描述的解决方案似乎已经为我解决了这个问题。
标签: spring-boot spring-mvc swagger swagger-ui springfox