You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 07:02:44