有什么方法可以将Schema
隐藏在Responses
和Request body
部件中吗?我们只需要向Example Value
展示。我们使用OpenAPI 3。
依赖关系:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
我们可以通过在listed schema
文件中使用springdoc.swagger-ui.defaultModelsExpandDepth=-1
隐藏application.properties部件。
但是我们希望从Request Body
和Responses
中删除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))
但仍然无法对Schema
部件进行Request Body
隐藏。
发布于 2022-06-09 00:34:32
我不认为这可以通过注释来解决。
您可以预定义swagger css以隐藏您想要的元素。
为了达到这个目的,首先检查一下您使用的是哪个版本的swagger。在我的例子中,是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,如下图所示:
https://stackoverflow.com/questions/72532273
复制相似问题