小编81n*_*1bK的帖子

如何注释 DTO 以使其显示在 SwaggerUI 架构中?

我有一个带有@RequestBodyDTO 的控制器。我需要显示 DTO 的架构,而不是stringSwagger 中 RequestBody 架构中的默认架构。

通过在 API 之上使用 @Operation 并在其中使用 @Parameter,我已经能够在两个地方描述 DTO

图片描述

并填写示例(参见代码)。我已经尝试@Schema@Operation(在 requestBody 下)和@Parameter注释。前者抛出 NPE,后者不做任何改变,并针对 DTO 本身中的对应注释进行了各种尝试。

样品控制器

@RequestMapping(value = "/{myPathVar}", method = RequestMethod.POST)
@Operation(summary = "Create something.", 
    parameters = { @Parameter(in = ParameterIn.PATH, name = "myPathVar", description = "Some path variable. Swagger uses this description.") },             
    requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
        description = "My description here.", 
        content = @Content(examples = @ExampleObject("{\"A\" : \"a\",\"B\" : \"{\"b\" : \"foo\", \"bb\" …
Run Code Online (Sandbox Code Playgroud)

swagger spring-boot springdoc springdoc-openapi-ui

5
推荐指数
1
解决办法
2万
查看次数