M12*_*123 7 java swagger-ui spring-boot openapi springdoc-openapi-ui
有什么办法可以隐藏和Schema零件吗?我们只需要展示。我们使用 OpenAPI 3。ResponsesRequest bodyExample Value
依赖关系:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我们可以通过在 application.properties 文件中使用来隐藏listed schema部分springdoc.swagger-ui.defaultModelsExpandDepth=-1。
但我们想从Request Body和中删除 API 模式部分Responses。
我尝试过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)))
})
Run Code Online (Sandbox Code Playgroud)
请求正文代码:
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content= @Content(schema = @Schema(example="JsonRequestBody...")))
Run Code Online (Sandbox Code Playgroud)
谁能建议我们如何做到这一点?
我们可以Schema从响应中隐藏该部分,如下所示。
@ApiResponse(responseCode = IConstants.R_str_200, content = @Content(examples=
@ExampleObject(name="SUCCESS RESPONSE",value="Json response..."),
mediaType = IConstants.MEDIA_JSONVALUE))
Run Code Online (Sandbox Code Playgroud)
但仍然无法隐藏Schema部分Request Body。
我认为这不能通过注释来解决。
您可以预定义 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"));
}
}
Run Code Online (Sandbox Code Playgroud)
加载 css 时将调用此控制器的端点。本质上,这里拦截了加载的css,并添加了自定义的css来隐藏你想要的元素。
通过此更改,当您启动应用程序并转到端点查看 swagger 文档时,您应该看到如下图所示的 UI:
| 归档时间: |
|
| 查看次数: |
5770 次 |
| 最近记录: |