【问题标题】:Springdoc with a generic return type具有通用返回类型的 Springdoc
【发布时间】:2022-01-17 20:18:39
【问题描述】:

当用户使用 Pageable 进行查询时,我有一个自定义页面要返回。这个页面有一个通用参数,我想在我的 Swagger 文档中指定它。 这是当前的结果:

elements 是我的泛型类型的实例列表。

我实际拥有的签名功能:

@CustomPageableAsQueryParam
    @Operation(summary = "", operationId = "get_all_version")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Versions found an returned",
                    content = @Content(mediaType = "application/json", schema = @Schema(implementation = CustomPage.class)))
    })
    @GetMapping(path = "/versions", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CustomPage<ReducedVersion>> getVersions(
            @ParameterObject
            @PageableDefault(sort = {"releaseTime"}, direction = Sort.Direction.DESC) Pageable pageable,
            @Parameter(description = "Version type to filter the list") @RequestParam(required = false) VersionType type) {

理想情况下,我可以使用@Schema(implementation = CustomPage&lt;ReducedVersion&gt;.class),但在 Java 中这是不可能的。

Spring 是否提供了克服此限制的方法?

感谢您以后的回答!

【问题讨论】:

    标签: java swagger spring-restdocs springdoc


    【解决方案1】:

    解决这个问题的方法是创建一个新类来包装该类型:

    public class ReducedCustomPage extends CustomPage<ReducedVersion> {
    
    }
    

    那么您的控制器将如下所示:

    @CustomPageableAsQueryParam
        @Operation(summary = "", operationId = "get_all_version")
        @ApiResponses(value = {
                @ApiResponse(responseCode = "200", description = "Versions found an returned",
                        content = @Content(mediaType = "application/json", schema = @Schema(implementation = ReducedCustomPage.class)))
        })
        @GetMapping(path = "/versions", produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<ReducedCustomPage> getVersions(
                @ParameterObject
                @PageableDefault(sort = {"releaseTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                @Parameter(description = "Version type to filter the list") @RequestParam(required = false) VersionType type) {
    

    【讨论】:

    • 谢谢,解决方案并不完美,但我会采用它,因为没有更好的解决方案!
    猜你喜欢
    • 1970-01-01
    • 2015-12-28
    • 2019-09-21
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多