简体   繁体   English

带有 springdoc-openapi-ui 的 Swagger UI 不显示生成的“@Controller”类中的 API

[英]Swagger UI with springdoc-openapi-ui doesn't show the APIs in the generated "@Controller" class

I have a Spring Boot application where the API is specified as a OpenAPI 3.0.2 YAML document.我有一个 Spring Boot 应用程序,其中 API 被指定为 OpenAPI 3.0.2 YAML 文档。 I used the openapi-generator-maven-plugin to generate code from the spec.我使用 openapi-generator-maven-plugin 从规范中生成代码。 When I open up http://localhost:8080/swagger-ui.html, it displays: "No operations defined in spec!"当我打开 http://localhost:8080/swagger-ui.html 时,它显示:“规范中没有定义任何操作!”

In the spec, I have:在规范中,我有:

servers:
    - url: /books/api/v1

Which results in this in the controller class:这导致控制器类:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2021-06-17T14:52:43.686820-07:00[America/Los_Angeles]")
@Controller
@RequestMapping("${openapi.axmPlatformKeyService.base-path:/books/api/v1}")
public class SvcApiController implements SvcApi {
    // ...
    // ...
}

If I load the openapi-definition.yaml in editor.swagger.io, it shows the definitions as expected.如果我在 editor.swagger.io 中加载 openapi-definition.yaml,它会按预期显示定义。

If I create another controller this way:如果我以这种方式创建另一个控制器:

@RestController
public class AddonController implements SvcApi {
    // ...
    // ...
}

then Swagger UI shows the APIs, which basically means that if the generated code had "@RestController" it would have worked okay.然后 Swagger UI 显示 API,这基本上意味着如果生成的代码具有“@RestController”,它就会正常工作。

Since the generated controller is annotated with @Controller, Swagger UI is not able to pick it up.由于生成的控制器是用@Controller 注释的,Swagger UI 无法获取它。 Even after adding this extra @RestController, Swagger UI's Try it out function doesn't include "/books/api/v1" in the URL it generates.即使在添加了这个额外的 @RestController 之后,Swagger UI 的 Try it out 功能也不在它生成的 URL 中包含“/books/api/v1”。

Net-effect is with this AddonController, if I have one request /book/{id} in the spec, there are two endpoints in the service: Net-effect 与此 AddonController 一起使用,如果我在规范中有一个请求 /book/{id},则服务中有两个端点:

/books/api/v1/book/{id} /book/{id} /books/api/v1/book/{id} /book/{id}

and the latter is invoked by Swagger UI.后者由 Swagger UI 调用。

These are the relevant dependencies:这些是相关的依赖项:

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

    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <version>1.6.2</version>
    </dependency>

Adding a static initializer that does添加一个静态初始值设定项

SpringDocUtils.getConfig().addRestControllers(SvcApiController.class) 

resolves the issue.解决了这个问题。 The AddonController is not needed in this case.在这种情况下不需要 AddonController。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swagger 当 SpringBoot 在 JAR 中运行时,springdoc-openapi-ui 不起作用 [已解决] - Swagger springdoc-openapi-ui doesn't work when SpringBoot run in JAR [SOLVED] Spring Boot 3 springdoc-openapi-ui 不工作 - Spring Boot 3 springdoc-openapi-ui doesn't work springdoc-openapi-ui生成的swagger-ui中如何更改请求header中授权key的名称 - How to change the name of the authorization key in the request header in the swagger-ui generated by springdoc-openapi-ui 带有 springdoc-openapi-ui 的 swagger-codegen-maven-plugin - swagger-codegen-maven-plugin with springdoc-openapi-ui Gradle 对 springdoc-openapi-ui 的依赖 - Gradle dependency for springdoc-openapi-ui springfox 到 springdoc-openapi-ui 迁移 - springfox to springdoc-openapi-ui migration 使用 Spring 安全性启用 Swagger springdoc-openapi-ui (OpenAPI 3.0) - 无法访问 swagger-ui.html (401) - Enabling Swagger springdoc-openapi-ui (OpenAPI 3.0) with Spring Security - Cannot access swagger-ui.html (401) springdoc-openapi-ui 与 openapi-generator-maven-plugin 不兼容 - springdoc-openapi-ui not compatible with openapi-generator-maven-plugin springdoc-openapi-ui OpenAPI Java 生成器 Inheritance - Springdoc-openapi-ui OpenAPI Java Generator Inheritance 授权 Header 未在 CURL springdoc-openapi-ui 中显示 - Authorization Header not getting displayed in CURL springdoc-openapi-ui
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM