繁体   English   中英

如何在 Spring Boot 中使用 OpenAPI 3 从“响应”和“请求正文”中隐藏“模式”?

[英]How to hide "Schema" from "response" and "Request body" using OpenAPI 3 in Spring Boot?

有没有办法从ResponsesRequest body部分隐藏Schema 我们只需要显示Example Value 我们使用 OpenAPI 3。

依赖:

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-ui</artifactId>
   <version>1.6.9</version>
</dependency>

我们可以通过在 application.properties 文件中使用springdoc.swagger-ui.defaultModelsExpandDepth=-1来隐藏listed schema部分。

在此处输入图像描述

但我们想从Request BodyResponses中删除 API 模式部分。

在此处输入图像描述

我尝试content= @Content(schema = @Schema(hidden = true ))但它隐藏了整个请求正文/响应。

在此处输入图像描述

响应代码:

@ApiResponses({
            @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(name = "Success response", example = "JsonResponse..."),
                    mediaType = MediaType.APPLICATION_JSON_VALUE)),
            @ApiResponse(responseCode = "400", description = "BAD REQUEST", content = @Content(schema = @Schema(hidden = true))) 
    })

请求正文的代码:

@io.swagger.v3.oas.annotations.parameters.RequestBody(
            content= @Content(schema = @Schema(example="JsonRequestBody...")))

谁能建议我们如何做到这一点?

更新:

我们可以从响应中隐藏Schema部分,如下所示。

@ApiResponse(responseCode = IConstants.R_str_200, content = @Content(examples=
@ExampleObject(name="SUCCESS RESPONSE",value="Json response..."),
                mediaType = IConstants.MEDIA_JSONVALUE))

在此处输入图像描述

但仍然无法从Request Body中隐藏Schema部分。

我认为这不能使用注释来解决。

你可以预定义 swagger css 来隐藏你想要的元素。

为此,首先检查您使用的是哪个版本的 swagger-ui。 就我而言,它是3.25.0. 您可以通过转到External Libraries文件夹(如果您使用 InteliJ)来检查您使用的版本,您应该在那里找到它(见下图)

在此处输入图像描述

然后,像这样编写控制器类:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@RestController
@RequestMapping(path = "/swagger-ui")
public class SwaggerController {
    @GetMapping(path = "/swagger-ui.css", produces = "text/css")
    public String getCss() {
        String orig = toText(getClass().getResourceAsStream("/META-INF/resources/webjars/swagger-ui/3.25.0/swagger-ui.css"));
        String customCss = "li.tabitem.active {\n" +
                "    display:block !important;\n" +
                "}\n" +
                "li.tabitem {\n" +
                "    display:none !important;\n" +
                "}}";
        return  orig+customCss;
    }


    static String toText(InputStream in) {
        return new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
                .lines().collect(Collectors.joining("\n"));
    }
}

加载 css 时将调用此控制器的端点。 本质上就是这里截取了加载的css,添加了一个自定义的css来隐藏你想要的元素。

通过此更改,当您启动应用程序并转到端点查看 swagger 文档时,您应该会看到如下图所示的 UI:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM