简体   繁体   English

如何将 Swagger 与 SpringDoc YAML 集成?

[英]How to integrate Swagger with SpringDoc YAML?

I'm using Swagger to document my project.And I want generate the YAML doc from springdoc.我正在使用 Swagger 来记录我的项目。我想从 springdoc 生成 YAML 文档。 But when I generate this YAML documentation the YAML dont have my Swagger doc coments.但是当我生成这个 YAML 文档时,YAML 没有我的 Swagger 文档注释。 For example.例如。 I have one endpoint in my project:我的项目中有一个端点:

@ApiOperation(value = "Return a list of Pix Wallets.", httpMethod = "POST", response = DResponse.class)
@PostMapping("/digital-wallet")
public ResponseEntity<DResponse> getDigitalWallets(@RequestBody PixDigitalWalletRequest pixDigitalWalletRequest) {
    return ResponseEntity.ok(pixService.getDigitalWallets(pixDigitalWalletRequest));
}

When I open my swagger doc I can see the correct documentation:当我打开我的 swagger 文档时,我可以看到正确的文档:

在此处输入图片说明

But... When I generate my YAML doc I don't see my comment (like: "Return a list of Pix Wallets.") in my YAML doc.但是...当我生成 YAML 文档时,我在 YAML 文档中看不到我的评论(例如:“返回 Pix 钱包列表。”)。 For example:例如:

paths:
   /api/pix/digital-wallet:
      post:
         tags:
         - pix-controller
  operationId: getDigitalWallets
  requestBody:
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/PixDigitalWalletRequest'
  responses:
    "200":
      description: default response
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/DResponse'

How can add my Swagger comments in my YAML doc?如何在我的 YAML 文档中添加我的 Swagger 评论?

You're facing the issue cause you're using Swagger 1.x annotation with Springdoc which relies on Swagger 2.x annotations.您正面临这个问题,因为您将 Swagger 1.x 注释与依赖 Swagger 2.x 注释的 Springdoc 一起使用。

Refactor your code as below to solve the issue如下重构您的代码以解决问题

@Operation(summary = "Return a list of Pix Wallets.")
@ApiResponses(value = {
        // 201 as it's a POST method, ideally shoud have empty schema as @Schema(), but put the class name to suit your use-case
        @ApiResponse(responseCode = "201", description = "Created", content = {@Content(mediaType = "application/json", schema = @Schema(DResponse.class))}),
        @ApiResponse(responseCode = "500", description = "Internal Server Error", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = MyErrorResponse.class))})
})
@PostMapping("/digital-wallet")
public ResponseEntity<DResponse> getDigitalWallets(@RequestBody PixDigitalWalletRequest pixDigitalWalletRequest) {
    return ResponseEntity.ok(pixService.getDigitalWallets(pixDigitalWalletRequest));
}

Refer to the Migrating from Springfox - Springdoc page for a detailed list of all the annotations and other migration changes.有关所有注释和其他迁移更改的详细列表,请参阅从 Springfox 迁移 - Springdoc页面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM