Pra*_*ran 8 swagger spring-boot springdoc springdoc-ui
我正在尝试创建springdocswagger 文档,并且我想Map<String, Object>以更好的客户端可读方式表示具有数据类型的请求正文。但是当我声明@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class)架构如下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);
Run Code Online (Sandbox Code Playgroud)
虽然Spring boot会根据方法签名自动推断类型,但对于数据类型并不清楚Map。例如,默认情况下,类型 Map<String, Object> 将被推断如下

但我想以更容易理解的方式向引用我的 API 的客户展示架构。我可以看到Github中有一个没有正确解决方案的已关闭票证。根据我的要求,请求正文应该是类型不可知的动态键值对,因此除了将请求接收为Map<String, Object>. 有没有人用类型实现了更好的方法Map而不是创建自定义请求/响应模型?
分享我对这个问题的工作方法,我已经为@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Map.class)架构作为字符串问题做了一个解决方法。
我在 OpenAPI bean 声明中声明了一个名为 Map 的自定义模式,如下所示
new OpenAPI()
.components(new Components()
.addSchemas("Map", new Schema<Map<String, Object>>().addProperties("< * >", new ObjectSchema())
))
.....
.....
Run Code Online (Sandbox Code Playgroud)
并在架构声明中使用上述架构,如下所示
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @Content(mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(ref = "#/components/schemas/Map"))
Run Code Online (Sandbox Code Playgroud)
ApiResponse上面的方法也可以用来代替下面的方法
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200",
content = @Content(mediaType = APPLICATION_JSON_VALUE,
schema = @Schema(ref = "#/components/schemas/Map"))
Run Code Online (Sandbox Code Playgroud)
注意:如果我们使用上述自定义模式方法,我们不需要更改或忽略SpringDoc内部使用的任何类型。
小智 7
我有一个 API 端点,请求正文需要一个 HashMap。关于如何解决“示例值”问题的信息并不多。 Prasanth 的回答将我带到了正确的地方。为了完整性,我发布了我的解决方案,但所有功劳都归于他。(PS:我试图投票给他的答案,但我没有足够的“点”)
配置方面:
@Configuration
@OpenAPIDefinition
public class DocsConfiguration {
@Bean
public OpenAPI customOpenAPI() {
Schema newUserSchema = new Schema<Map<String, Object>>()
.addProperties("name",new StringSchema().example("John123"))
.addProperties("password",new StringSchema().example("P4SSW0RD"))
.addProperties("image",new StringSchema().example("https://robohash.org/John123.png"));
return new OpenAPI()
//.servers(servers)
.info(new Info()
.title("Your app title")
.description("App description")
.version("1.0")
.license(new License().name("GNU/GPL").url("https://www.gnu.org/licenses/gpl-3.0.html"))
)
.components(new Components()
.addSchemas("NewUserBody" , newUserSchema)
);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器端:
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(ref = "#/components/schemas/NewUserBody")))
@PostMapping("/v1/users")
public Response<User> upsertUser(@RequestBody HashMap<String,Object> user) {
//Your code here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30331 次 |
| 最近记录: |