【发布时间】: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