我正在尝试使用 Swagger UI 创建和部署我的文档以及我使用 Spring Boot 编写的 API。我知道 Swagger 提供了一些注释来帮助在实际控制器类中编写文档,但我在尝试让它们执行我需要的操作时遇到了麻烦。
我的问题是我有一个通用的 DTO 类,每次调用我的 API 都会返回该类。此 DTO 有一个contents用于通用对象的字段。如果我直接使用这些对象,我知道我可以使用类似的东西
@ApiResponse(responseCode = "200", description = "Customer found",
content = @Content(
schema = @Schema(implementation = Customer.class)))
Run Code Online (Sandbox Code Playgroud)
以便给出对象的 JSON 表示形式的规范。但是,因为我将所有内容包装在特定的ResponseDTO类中,所以我需要一种方法来指定字段contents应该是什么样子,并且我不确定可以使用哪些注释来完成此任务。我觉得肯定应该有类似的东西
@ApiResponse(responseCode = "200", description = "Customer found",
content = @Content(
schema = @Schema(implementation = DTO.class,
fields = { "contents" = @Schema(implementation = Customer.class)})))
Run Code Online (Sandbox Code Playgroud)
或类似的东西。我一直无法找到如何真正实现这一目标的解释。我的直觉表明应该有一种方法可以将模式放入模式中,但也许还有另一种我没有考虑过的解决方案。任何帮助或寻找方向将不胜感激。提前致谢。