【发布时间】:2021-03-08 15:19:55
【问题描述】:
我正在尝试创建springdoc swagger 文档,并且我想以更易读的方式为客户表示具有数据类型Map<String, Object> 的请求正文。但是当我声明@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class) 时,Schema 会以String 的形式出现(附上下面的屏幕截图)
方法声明
@Operation(security = {@SecurityRequirement(name = "bearer-key")}, summary = "Create Data", operationId = "createData", description = "Create createData for the **`type`**. ")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Data created", content = @Content(schema = @Schema(implementation = Map.class),
examples = {@ExampleObject(value = "{\n" +
" \"id\": \"927d810c-3ac5-4584-ba58-7c11befabf54\",\n" +
"}")})),
@ApiResponse(responseCode = "400", description = "BAD Request")})
@PostMapping(value = "/data/type", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class),
examples = {@ExampleObject(value = "{\n" +
" \"label\":\"tourism\",\n" +
" \"location\":\"France\"\n" +
" }")}))
ResponseEntity<Map<String, Object>> createData(@Parameter(name = "type", required = true) @PathVariable("type") String type, @Parameter(name = "request payload") @Valid @RequestBody Map<String, Object> body);
虽然Spring boot会根据方法签名自动推断出类型,但是对于Map这个数据类型还不清楚。例如,默认情况下,类型 Map
但我想以一种更易于理解的方式向引用我的 API 的客户展示 Schema。我可以看到Github 中有一张没有适当解决方案的封闭票。根据我的要求,请求正文应该是类型不可知的动态键值对,因此除了以Map<String, Object> 接收请求之外别无他法。有没有人使用Map 类型实现了更好的方法,而不是创建自定义请求/响应模型?
【问题讨论】:
标签: spring-boot swagger springdoc springdoc-ui