环境

springboot: 2.7.15
swagger2: 2.9.2
swagger-ui: 2.9.2

error

空指针问题

1
2
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:

解决办法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Springboot2.6.x以上的避免空指针异常,在启动类加注解@EnableWebMvc
访问接口文档页面404的,可以配置一个实现WebMvcConfigurer的类
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
/**
* 配置swagger-ui显示文档
*/
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}

}