OpenAPI Generator生成的Spring Boot接口在Swagger UI中重复显示问题
解决方案
1. 规范OpenAPI YAML的标签定义
确保YAML中仅在目标接口层级单次关联标签,避免全局标签与接口标签重复定义导致映射冲突。示例写法:
openapi: 3.0.3 info: title: Demo API version: 1.0.0 paths: /table: get: tags: - zuege summary: 获取表格数据 responses: '200': description: 成功返回数据 # 若需全局标签描述,仅定义一次即可,不要重复关联到接口 tags: - name: zuege description: 车次相关接口
2. 调整openapi-generator-maven-plugin配置
在插件配置中添加<removeOperationIdPrefix>true</removeOperationIdPrefix>,配合<useTags>true</useTags>避免因OperationId前缀重复导致接口被多次注册。完整配置示例:
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>6.3.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec> <generatorName>spring</generatorName> <apiPackage>com.example.api</apiPackage> <modelPackage>com.example.model</modelPackage> <useTags>true</useTags> <removeOperationIdPrefix>true</removeOperationIdPrefix> <configOptions> <springBoot3>true</springBoot3> <interfaceOnly>true</interfaceOnly> </configOptions> </configuration> </execution> </executions> </plugin>
3. 排查自定义OpenAPI Bean配置
若手动配置了OpenAPI Bean,确保没有重复扫描接口或重复添加路径定义,全局标签仅需定义一次:
@Configuration public class OpenApiConfig { @Bean public OpenAPI customOpenAPI() { return new OpenAPI() .info(new Info().title("Demo API").version("1.0.0")) .tags(List.of(new Tag().name("zuege").description("车次相关接口"))); } }
4. 清理残留代码并重新构建
执行命令清理旧生成代码,避免残留文件导致重复注册:
mvn clean install
内容的提问来源于stack exchange,提问作者kladderradatsch
相关产品推荐
相关产品推荐

