如何使用 springdoc 在 swagger openapi 规范 3.0 @RequestBody中创建多个模式?



我有以下 api,我需要有两个内容类型application/x-www-form-urlencoded的参数,因此使用@RequestBody而不是@Parameter


@Operation(summary = "Revoke given permissions", description = "Allows admin to revoke permissions to users")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void revokePermission(
@RequestBody(description = "the permission id", content = @Content(mediaType = "application/x-www-form-urlencoded",
schema = { @Schema(type = "String", name = "permission_id",
description = "id of the permission to be revoked", required = true)},
{ @Schema(type = "String", name = "permission_type", 
description = "the permission type")})) 
String permission_id, String permissionType) {
do_something();
}

我需要 swagger.json 像下面的示例一样,但我不知道如何使用 springdoc 生成它。我也尝试了@ArraySchema,但我没有得到我需要的输出。我在语法上犯了一些错误,无法在网上找到示例。

"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"properties": {
"permission_id": { 
"description": "id of the permission to be revoked",
"type": "string"
},
"permission_type": {
"description": "the permission type",
"type": "string"
}
},
"required": ["permission_id"] 
}
}
}
}

任何帮助都非常感谢。蒂亚

实现所需内容的最简单方法是在简单对象中定义权限数据,如下所示:

@Schema(name = "permissionData")
public class PermissionData {
@Schema(type = "String", name = "permiddionId", description = "id of the permission to be revoked", required = true)
@JsonProperty("permiddionId")
String permiddionId;
@Schema(type = "String", name = "permissionType",description = "the permission type")
@JsonProperty("permissionType")
String permissionType;
}

然后你控制器方法:

@Operation(summary = "Revoke given permissions", description = "Allows admin to revoke permissions to users")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void revokePermission(@RequestBody(description = "the permission data") PermissionData permissionData) {
}

相关内容

  • 没有找到相关文章

最新更新