无法让 swagger2 显示 swagger-ui.html

发布于 2025-01-14 08:40:50 字数 1667 浏览 1 评论 0原文

问题

正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2025-01-21 08:40:50

为了使用 spring-fox 启用 swagger ui,您需要在 pom.xml 中添加额外的依赖项。

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

(这是最新版本,您可以根据用例使用任何版本)

swagger UI 的路径将位于以下链接或使用 swagger-ui.html 页面。

http://localhost:8080/your-app-root/swagger-ui/

In order to enable swagger ui using spring-fox, you need to add an additional dependency in your pom.

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

(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.

http://localhost:8080/your-app-root/swagger-ui/
笑咖 2025-01-21 08:40:50

我有类似的问题。解决方案是为控制器添加 URL 映射。像这样:

@RequestMapping("/myprefix")
@RestController
public class Controller {...}

否则,它将解析为默认值,即“/”。不知何故,http://localhost:8080/your-app-root/swagger-ui/ 试图解析为拦截所有以“/”开头的路径 url 的控制器。如果如上所述将其更改为“/myprefix”,则 http://localhost:8080/your-app-root/swagger-ui/ 将不会被该控制器拦截。
作为依赖项,我使用了以下内容:

 <dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.13</version>
 </dependency>

但上述解决方案也适用于

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency> 

显示 swagger-ui.html 所需的一切。

I had a similar issue. The solution was to add a URL mapping for the controller. like this:

@RequestMapping("/myprefix")
@RestController
public class Controller {...}

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:

 <dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.13</version>
 </dependency>

But the above solution would also work for

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency> 

Thats all you need to display swagger-ui.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文