【问题标题】:How to create multiple schema in @RequestBody of swagger openapi specification 3.0 using springdoc?如何使用springdoc在swagger openapi规范3.0的@RequestBody中创建多个模式?
【发布时间】:2020-02-01 20:37:36
【问题描述】:

我有以下 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"] 
        }
      }
    }
  }

非常感谢任何帮助。 TIA

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。但是如果不创建一个对象来获得许可,就没有办法实现这一点吗?我希望它们是单独的变量
    猜你喜欢
    • 2020-08-30
    • 2021-06-03
    • 2018-12-05
    • 2019-09-07
    • 2020-05-22
    • 2021-11-14
    • 2020-04-21
    • 1970-01-01
    • 2020-09-09
    相关资源
    最近更新 更多