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

如何移除OpenAPI Swagger 3.0规范默认引入的冗余端点?

解决SpringBoot集成SpringDoc-OpenAPI冗余端点问题

冗余端点的来源

你当前的问题主要来自两个方面:

  1. 同时引入了springdoc-openapi-ui(适配Spring MVC的OpenAPI工具)和swagger-jaxrs2(适配JAX-RS的Swagger工具),两个依赖会各自扫描API并生成文档,导致出现JAX-RS相关的冗余默认端点。
  2. springdoc默认会自动识别并展示Spring Boot Actuator端点(若项目引入Actuator),以及自身的API文档端点,同时你配置的paths-to-exclude可能因路径匹配或配置逻辑问题未生效。

具体解决步骤

1. 移除不必要的依赖

由于你使用的是Spring MVC的@RestController,完全不需要swagger-jaxrs2依赖,它会额外生成JAX-RS相关的冗余端点文档。修改build.gradle,删除这条依赖:

// 删掉这条冗余依赖
// implementation 'io.swagger.core.v3:swagger-jaxrs2:2.2.6'

2. 修正SpringDoc配置,精准控制API范围

你的paths-to-exclude未生效,更稳妥的方式是明确指定需要包含的路径,而非仅排除,这样能彻底过滤所有非业务API。修改application.yml:

springdoc:
  swagger-ui:
    path: /swagger-ui.html
    query-config-enabled: true
  api-docs:
    path: /api/openapi.json
    enabled: true # 若要Swagger UI正常加载,需开启此选项,否则UI无法获取API元数据
  paths-to-include:
    - /addresses/** # 只展示你的业务API路径
  paths-to-exclude:
    - /actuator/** # 若项目有Actuator,排除其端点
    - /api/healthcheck # 排除自定义健康检查路径

3. 规范Spring MVC注解

你代码中使用了JAX-RS的@GET注解,这是因引入swagger-jaxrs2导致的混用,换成Spring MVC原生的@GetMapping更规范:

@RequestMapping("/addresses")
@RestController
@PermitAll
@Tag(name="Addresses",description="Addresses API")
public class AddressRest {

    @Autowired
    private AddressesService addressService;

    @GetMapping
    @Produces(MediaType.APPLICATION_JSON)
    public List<AddressDTO> findAll() {
        return this.addressService.findAll();
    }
}

4. 进阶:自定义OpenAPI过滤(若仍有冗余)

如果以上步骤后仍有冗余端点,可以通过自定义OpenApiCustomiser手动过滤:

@Configuration
public class OpenApiConfig {
    @Bean
    public OpenApiCustomiser redundantPathFilter() {
        return openApi -> openApi.getPaths().keySet().removeIf(
                path -> path.startsWith("/actuator/") 
                        || path.equals("/api/healthcheck")
                        || path.startsWith("/v3/api-docs/") // 若需要排除SpringDoc自身的文档端点
        );
    }
}

内容的提问来源于stack exchange,提问作者zweibit01101

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 06:41:19