无法让 swagger2 显示 swagger-ui.html
问题
正在学习java spring boot,我的问题是从 http://localhost:8080/swagger-ui.html#/ 加载 swagger 前端 我收到如下控制台消息:
WARN 23432 --- [nio-8080-exec-9] osweb.servlet.PageNotFound : No mapping for GET /swagger-ui.html
背景
我构建的 使用带有基本 API 的 spring boot 构建一个入门项目,并使用 postman 测试了端点。 我正在使用 spring-boot-starter-parent v2.6.4。
我是第一次尝试 swagger,并在我的 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);
}
The problem
Am learning java spring boot and my problem is getting the swagger front-end to load from http://localhost:8080/swagger-ui.html#/
I get the console message as follows:
WARN 23432 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound : No mapping for GET /swagger-ui.html
Background
I've built out a starter project using spring boot with a basic API and have tested the endpoints with postman ok.
I'm using v2.6.4 of spring-boot-starter-parent.
I'm trying out swagger for the first time and have included the following in my pom.xml
groupId io.springfox
artifactId springfox-boot-starter
version 3.0.0
In my application.yml I have added the following to resolve a build issue which was related to a version/dependency mismatch.
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
I've added the following class to my config package based on a tutorial I am following.
@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();
}
I found some articles saying to override resource handling as follows to cure the problem but it is not helping:
@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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了使用 spring-fox 启用 swagger ui,您需要在 pom.xml 中添加额外的依赖项。
(这是最新版本,您可以根据用例使用任何版本)
swagger UI 的路径将位于以下链接或使用 swagger-ui.html 页面。
In order to enable swagger ui using spring-fox, you need to add an additional dependency in your pom.
(This is the latest version, you can use any version based on the usecase)
The path of the swagger UI will be at the below link or using swagger-ui.html page.
我有类似的问题。解决方案是为控制器添加 URL 映射。像这样:
否则,它将解析为默认值,即“/”。不知何故,http://localhost:8080/your-app-root/swagger-ui/ 试图解析为拦截所有以“/”开头的路径 url 的控制器。如果如上所述将其更改为“/myprefix”,则 http://localhost:8080/your-app-root/swagger-ui/ 将不会被该控制器拦截。
作为依赖项,我使用了以下内容:
但上述解决方案也适用于
显示 swagger-ui.html 所需的一切。
I had a similar issue. The solution was to add a URL mapping for the controller. like this:
Otherwise, it would resolve to the default one, which is "/". Somehow http://localhost:8080/your-app-root/swagger-ui/ is trying to be resolved to the controller which is intercepting all paths url starting with "/". If you change it to "/myprefix" as mentioned above, http://localhost:8080/your-app-root/swagger-ui/ would not be intercepted by this controller.
As a dependency I used the following:
But the above solution would also work for
Thats all you need to display swagger-ui.html.