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

标签 spring-boot openapi swagger-3.0 springdoc springdoc-openui

我有下面的 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;
}

然后你的 Controller 方法:

@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) {

}

关于spring-boot - 如何使用 springdoc 在 swagger openapi 规范 3.0 的 @RequestBody 中创建多个模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996606/

相关文章:

java - 访问字段时出错(使用 Hibernate/JPA)

启动 tomcat 上下文的 Spring-boot 错误。创建名称为 'servletEndpointRegistrar' 的 bean 时出错

php - 如何修复 ErrorException : @OA\Items() is required when @OA\Property() has type "array"?

swagger - OpenAPI 架构的媒体类型是什么?

protocol-buffers - 如何从 protobuf (.proto) 文件在 (.json/.yaml) 中生成 swagger3 (OpenAPI3) 规范?

spring-boot - 当所有代理都关闭时 Spring-Kafka Producer 重试

java - 为什么 Spring boot Security Basic Authentication 很慢?

c# - 使用 c# SDK Azure.ResourceManager.ApiManagement 导出 OpenAPI 定义

java - 使用 springfox-swagger2 版本 3.0.0-SNAPSHOT 观察错误 "Unable to infer base url"

c# - AddMvc/AddSwaggerGen 和 UseMvc/UseSwagger(UI) 之间的顺序