问题
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:
翻译:
无法推断基本网址。当使用动态servlet注册或API位于API网关后面时,这很常见。基本url是为所有swagger资源提供服务的根。例如如果api在http://example.org/api/v2/api-docs上可用,则基本URL为http://example.org/api/。请手动输入位置:
原因可能
1. Swagger配置类的注解未写
@EnableSwagger2注解问题
import ...;
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket productApi() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2).select()
// 扫描的包路径
.apis(RequestHandlerSelectors.basePackage("cn.wolfcode.controller"))
// 定义要生成文档的Api的url路径规则
.paths(PathSelectors.any())//any表示所有的
.build()
.globalOperationParameters(pars)
// 设置swagger-ui.html页面上的一些元素信息。
.apiInfo(metaData());// api文档的一些属性信息
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
// 标题
.title("狼行天下")
// 描述
.description("狼行天下项目接口文档")
// 文档版本
.version("1.0.0")
.license("Apache License Version 2.0")
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
.build();
}
//ui页面
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
2. 被拦截器拦截了
要在配置中放行Swagger的页面
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(checkLoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")
;
registry.addInterceptor(brushProofInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")
;
registry.addInterceptor(signInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")
;
}