Spring Boot 集成 Swagger,启动后使用浏览器访问弹出提示框:
详细错误信息:
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:
Maven 依赖如下:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
查看是否在启动类中定义 @EnableSwagger2 注解
是否映射了 Swagger 的 webjars 资源。如果你通过继承 WebMvcConfigurationSupport 类实现资源映射,一定要注意一个 Spring Boot 应用中只允许出现一个 WebMvcConfigurationSupport 类(即使你配置了多个 WebMvcConfigurationSupport 类也只有一个会被执行),配置如下:
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 映射webjars 资源 registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); }