【问题标题】:How to annotate DTO so that it shows up in SwaggerUI Schema?如何注释 DATA 以使其显示在 Swagger UI Schema 中?
【发布时间】:2021-11-11 13:38:27
【问题描述】:

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

通过使用 API 上方的 @Operation 和内部的 @Parameter,我已经能够在两个地方描述 DTO

并填写示例(参见代码)。我在@Operation(在requestBody 下)和@Parameter 注释中尝试了@Schema。前者抛出一个 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\" : \"bar\"}"))))
@ApiResponse(content = @Content(schema = @Schema(implementation = MyReturningType.class)))
public MyReturningType doSomethingCool(
    @Parameter(description = "Some description Swagger ignores.", example = "123") @PathVariable(value = "myPathVar") int myPathVar,
    @Parameter(description = "My other description here.", schema = @Schema(implementation = MyDto.class)) @RequestBody MyDto dto) {
    // do something cool
}

DTO 示例

// Do I need certain annotations here?
public class MyDto {
    // What annotation goes here? @Parameter, @JsonProperty, @Schema, something else?
    private int someInt;
    private String someString;
    private Object someObject;
}

我需要哪种注释组合才能在 DTO 中正确标记 DTO Schema,然后从控制器引用此 Schema,以便在 SwaggerUI 中填充 Schema 字段?

【问题讨论】:

    标签: spring-boot swagger springdoc springdoc-openapi-ui


    【解决方案1】:

    问题可能是由于 DTO 中的字段具有私有可见性,并且从您共享的代码来看,它们似乎没有可用的 getter 和 setter。

    请参阅以下示例,了解如何完成此操作的工作示例

    控制器

    // Using the specific mapping annotation will keep the code clean
    @PostMapping("/{myPathVar}")
    
    // The below annotation describes the operation
    @Operation(
        summary = "Brief description of the operation",
        description = "Detailed description of the operation")
    
    // Describe the possible responses next. Prefer using @ApiResponses for multiple response
    @ApiResponse(
    
        // specify the http response code
        responseCode = "201",
    
        // some description. Maybe use the corresponding http response code's description
        description = "Created",
    
        // describe the content that will be returned for this response code
        content = @Content(
            // optionally, specify the media type for the response here as shown in the below code
             mediaType = MediaType.APPLICATION_JSON_VALUE,
            // specify the implementation of the response DTO here
            schema = @Schema(implementation = Void.class)))
    
    public Void doSomethingCool(
    
        // Use @Parameter for @PathVariable and @RequestVariable
        @Parameter(description = "Description for path/request-parameter here")
        @PathVariable(value = "myPathVar")
            int myPathVar,
    
        // Both these @RequestBody annotations are mandatory.
        @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "Controller-level model description here")
        @org.springframework.web.bind.annotation.RequestBody
            TestDTO dto) {
      // ... do some cool stuff here
      return null;
    }
    

    DTO

    @Schema(description = "Model-level description here")
    public class TestDTO {
    
      @Schema(description = "Field-level description here")
      private int someInt;
    
      @Schema(description = "Another Field-level description here")
      private String someString;
    
      @Schema(description = "Yet another Field-level description here")
      private Object someObject;
    
      // all getters and setters here
    
    }
    

    这将为您提供如下输出

    【讨论】:

    • 您的结果正是我正在寻找的结果,但是复制了您的解决方案后,我在请求正文下根本没有得到任何东西,甚至没有像以前那样的描述。我的 DTO 字段是私有的,但我确实有公共的 getter 和 setter。但是,DTO 和控制器在同一项目中的不同模块中,需要导入 DTO。我认为这可能是问题所在,但尝试使用公共 DTO 字段并将 DTO 与控制器置于同一级别,但仍然得到一个空的请求正文...
    • 您介意在 GitHub/Bitbucket 等上分享一个可重现的示例吗?
    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2021-10-30
    • 2014-12-26
    • 1970-01-01
    相关资源
    最近更新 更多