如何将Swagger与SpringDoc YAML集成?

时间:2020-10-23 16:12:29

标签: swagger swagger-ui springdoc springdoc-openui

我正在使用Swagger记录我的项目,并且我想从springdoc生成YAML文档。但是,当我生成此YAML文档时,YAML没有Swagger文档。例如。我的项目中有一个端点:

@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));
}

当我打开草率文档时,我可以看到正确的文档:

enter image description here

但是...当我生成YAML文档时,我的YAML文档中没有看到我的评论(例如:“返回Pix钱包列表。”)。例如:

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'

如何在YAML文档中添加Swagger注释?

1 个答案:

答案 0 :(得分:1)

您正面临问题,因为您将Swagger 1.x注释与Springdoc一起使用,而Swdoc 1.x注释依赖于Swagger 2.x注释。

按如下所示重构代码以解决问题

@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));
}

有关所有注释和其他迁移更改的详细列表,请参见Migrating from Springfox - Springdoc页面。

相关问题