【发布时间】:2022-12-13 23:14:50
【问题描述】:
问题
我正在学习 java spring boot,我的问题是让 swagger 前端从 http://localhost:8080/swagger-ui.html#/ 加载 我收到如下控制台消息:
WARN 23432 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound : No mapping for GET /swagger-ui.html
背景
我已经使用带有基本 API 的 spring boot 构建了一个入门项目,并使用邮递员测试了端点。 我正在使用 spring-boot-starter-parent v2.6.4。
我第一次尝试大摇大摆,并在我的 pom.xml 中包含以下内容
groupId io.springfox
artifactId springfox-boot-starter
version 3.0.0
在我的 application.yml 中,我添加了以下内容来解决与版本/依赖项不匹配相关的构建问题。
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
我已根据我正在关注的教程将以下类添加到我的配置包中。
@Configuration
@EnableWebMvc
@Import(SpringDataRestConfiguration.class)
public class ApplicationSwaggerConfig {
@Bean
public Docket speakersApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
我发现一些文章说要按如下方式覆盖资源处理来解决问题,但它没有帮助:
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
【问题讨论】:
-
这有时很奇怪,您可以在线尝试所有可能的解决方案。不断更改或降级 swagger 依赖项的版本,直到它被修复。这个建议并不理想,但对我一直有效。
标签: java spring-boot swagger-2.0 spring-boot-starter springfox-boot-starter