如何在Swagger UI中禁用所有操作下的示例与Schema渲染
禁用Swagger UI中「Example Value」和「Schema」板块的方法
1. 直接使用Swagger UI前端资源的场景
如果是自己引入Swagger UI静态文件搭建文档页面,初始化Swagger UI时配置两个关键参数就能隐藏目标板块:
defaultModelsExpandDepth: -1:隐藏右侧Models区域的所有展开内容(不需要隐藏这块的话,只配置下面的参数即可)defaultModelExpandDepth: -1:直接隐藏每个操作下方的「Example Value」和「Schema」板块
示例代码:
const ui = SwaggerUIBundle({ url: "你的Swagger接口规范文件地址.json", dom_id: '#swagger-ui', defaultModelsExpandDepth: -1, defaultModelExpandDepth: -1, // 其他原有配置... });
2. Spring Boot集成Swagger的场景
用Springfox(旧版Swagger集成方案)
两种配置方式二选一即可:
- 配置文件方式:在
application.properties或application.yml中添加:
springfox.documentation.swagger-ui.default-models-expand-depth=-1 springfox.documentation.swagger-ui.default-model-expand-depth=-1
- Java配置类方式:在Swagger配置类中添加
UiConfiguration的Bean:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.你的项目包名")) .paths(PathSelectors.any()) .build(); } @Bean public UiConfiguration uiConfig() { return UiConfigurationBuilder.builder() .defaultModelsExpandDepth(-1) .defaultModelExpandDepth(-1) .build(); } }
用SpringDoc(新版OpenAPI集成方案)
直接在配置文件中添加参数:
springdoc.swagger-ui.default-models-expand-depth=-1 springdoc.swagger-ui.default-model-expand-depth=-1
配置完成后重启服务,每个接口操作下方的「Example Value」和「Schema」板块就会完全消失。
内容的提问来源于stack exchange,提问作者Marty78
相关产品推荐
相关产品推荐

