如何覆盖 Swagger OpenApi 文档中的 Requestbody ExampleValue 和 Schema?

问题描述 投票:0回答:1

我在 Springboot (3.2.2) 中编写了一个 REST- Api,并且我正在尝试创建一个完全自定义的 Swagger OpenApi(3.0)。由于某些 api 要求,我的控制器方法中的 RequestBody 不能是稍后在代码中传入的 json 映射到的 dto 类。我仍然希望 dto 类成为我的 Swagger 文档中的“ExampleValue”和“Schema”。

我的控制器:

public ResponseEntity<Object> createSaveObjects(@RequestBody String json) {...}

为了自定义我的文档,我使用 SpringdocConfig - 类:

    @OpenAPIDefinition
    @Configuration

public class SpringdocConfig {

    @Bean
OpenAPI baseOpenAPI() {

    OpenAPI openAPI = new OpenAPI();

    // Info
    Info info = new Info()
            .title("Documentation")
            .description("blubb")
            .version("1.0.0");
    openAPI.setInfo(info);


    // Paths
    Paths paths = new Paths();
    paths.addPathItem("/object", new PathItem()
            .post(new Operation()
                    .summary("saves objects")
                    .description("saves objects")
                    .requestBody(new RequestBody()
                            .description("blibb")
                            .required(true)
                            .content(new Content()
                                    .addMediaType("application/json", new io.swagger.v3.oas.models.media.MediaType()
                                            .schema(new Schema()
                                                    .type("object")
                                                    .addProperty("Name", new Schema().type("string").example("string"))
                                                    .addProperty("Origin", new Schema().type("int").example(4))
                                                    
                                            )...

我想要什么:

    Request body

application/json
blibb

Example Value
{
  "Name": "string",
  "Origin": 4
}

我得到了什么:

  Request body

application/json
blibb

Example Value
"string"
spring-boot rest swagger openapi
1个回答
0
投票

我自己解决了这个问题:我刚刚将 @Hidden 注释添加到控制器中的 POST-Method 中,因此 Swagger 在文档中完全忽略它

@Hidden
@PostMapping
public ResponseEntity<Object> createSaveObjects(@RequestBody String json) {...}
© www.soinside.com 2019 - 2024. All rights reserved.