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

使用swagger-codegen-maven-plugin生成代码时如何移除springfox配置?

解决swagger-codegen生成Spring Boot代码时自动添加SpringFox的问题

我完全懂你现在的头疼点——用io.swagger.codegen.v3.swagger-codegen-maven-plugin 3.0.18从OpenAPI 3 YAML生成代码时,莫名其妙就带出了SpringFox的Swagger 2配置类和依赖,试了常规开关不管用,甚至连Spring Profiles都没法禁用,毕竟编译阶段就把那类给生成出来了。别慌,咱们直接从代码生成的源头下手解决:

步骤1:修改Maven插件配置,彻底禁用Swagger相关生成

你只需要在插件的<additionalProperties>里加上swagger=false这个配置项,它会直接告诉代码生成器:别生成任何和Swagger/SpringFox相关的代码、依赖。修改后的完整插件配置如下:

<configuration>
 <inputSpec>${swagger-codegen.api.spec}</inputSpec>
 <language>spring</language>
 <library>spring-boot</library>
 <apiPackage>${swagger-codegen.server.api.package}</apiPackage>
 <modelPackage>${swagger-codegen.server.model.package}</modelPackage>
 <addCompileSourceRoot>true</addCompileSourceRoot>
 <additionalProperties>
  <java8>true</java8>
  <additionalProperty>jackson=true</additionalProperty>
  <!-- 新增这行,彻底禁用Swagger相关生成 -->
  <swagger>false</swagger>
 </additionalProperties>
 <generateApiTests>false</generateApiTests>
 <generateModelTests>false</generateModelTests>
 <configOptions>
  <dateLibrary>java8</dateLibrary>
  <sourceFolder>.</sourceFolder>
  <useBeanValidation>true</useBeanValidation>
  <performBeanValidation>true</performBeanValidation>
  <delegatePattern>true</delegatePattern>
 </configOptions>
</configuration>

这个配置会同时搞定两个核心问题:

  • 不再生成io.swagger.configuration.SwaggerDocumentationConfig这个烦人的配置类
  • 生成的POM文件里会自动移除SpringFox的springfox-swagger2和springfox-swagger-ui依赖

为什么之前的springfox.enable=false没用?

那个配置是给SpringFox本身用的运行时开关,但你的问题是在代码生成阶段就自动创建了配置类和依赖,所以必须从代码生成器的配置入手,而不是Spring应用的运行时配置。

验证效果

修改完配置后,重新执行mvn swagger-codegen:generate,你会发现:

  • 生成的代码目录里再也找不到SwaggerDocumentationConfig类
  • 生成的POM文件里的SpringFox依赖已经被彻底移除
  • 编译阶段也不会再引入任何SpringFox相关的类了

如果之后想重新启用Swagger文档,只需要把<swagger>false</swagger>改回<swagger>true</swagger>就可以了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 00:02:37