首页 > 解决方案 > SwaggerUI 未显示由 OpenApi codegen 正确生成的记录 API

问题描述

我正在使用 openapi-generator-maven-plugin 从 YAML 文档生成代码。

openapi: 3.0.3
info:
  title: Example API Doc
  description: Those are example endpoints.
  termsOfService: https://localhost:8080/termsOfService
  license:
    name: No license
    url: https://localhost:8080/license
  version: 1.0-SNAPSHOT
servers:
- url: https://localhost:8080/
- url: http://localhost:8080/
tags:
- name: example
  description: Example Tag
paths:
  /example:
    get:
      tags:
      - example
      summary: Example GET request
      description: Send example GET request
      operationId: exampleGetRequest
      responses:
        200:
          description: Successful operation
          content: {}
        400:
          description: Example Bad Request
          content: {}
        404:
          description: Example Not Found
          content: {}

这就是我配置 maven 插件以生成接口的方式:

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.1.1</version>
  <configuration>
    <generatorName>spring</generatorName>
    <inputSpec>${project.basedir}/src/main/resources/exampleApi.yaml</inputSpec>
    <apiPackage>com.example.openapi.generated.api</apiPackage>
    <modelPackage>com.example.openapi.generated.models</modelPackage>
    <generateSupportingFiles>false</generateSupportingFiles>
    <output>${project.basedir}</output>
    <configOptions>
      <dateLibrary>java8</dateLibrary>
      <java8>true</java8>
      <interfaceOnly>true</interfaceOnly>
    </configOptions>
  </configuration>
</plugin>

这个插件很好用,我得到这样的界面

@Api(value = "example", description = "the example API")
public interface ExampleApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    @ApiOperation(value = "Example GET request", nickname = "exampleGetRequest", notes = "Send example GET request", tags = {"example",})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successful operation"),
            @ApiResponse(code = 400, message = "Example Bad Request"),
            @ApiResponse(code = 404, message = "Example Not Found")})
    @RequestMapping(value = "/example", method = RequestMethod.GET)
    default ResponseEntity<Void> exampleGetRequest() {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }
}

我正在用这个类实现我的接口:

@RestController
public class ExampleApiImpl implements ExampleApi {

    @Override
    public ResponseEntity<Void> exampleGetRequest() {
        return ResponseEntity.ok().build();
    }
}

我检查了解决方案,当我执行 api 调用时,我得到了 HTTP 状态 200,但是当我尝试访问 swagger-ui 页面时,没有记录这个 API 端点。有什么方法可以配置 OpenAPI UI 或 SwaggerUI 以指向我的 yaml 文档?

SwaggerUI 屏幕 在此处输入图像描述

标签: javaswaggerswagger-uiopenapiopenapi-generator

解决方案


如果它仍然是热门话题。

看起来您使用 Swagger 3 注释的 OpenAPI UI,但 OpenApi 生成器只能创建 Swagger 2 注释:Open API 代码生成器 Maven 插件使用旧的 Swagger 2 注释而不是 Swagger 3 注释

解决方案 #1 - 使用 Swagger v2 UI。移除 openApi 依赖并io.springfox:springfox-swagger2:{version}用于 UI 页面。

解决方案 #2 - 覆盖用于生成 v3 注释的 mustache 模板。


推荐阅读