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

openapi-generator-maven-plugin未生成@ExampleObject及Swagger UI示例求助

解决方案

1. 修复插件配置中的拼写错误

你当前的插件配置里<skipOperationExample>标签的闭合标签拼写错误(少了字母t),这会导致该配置失效,默认会跳过示例生成。修正后的配置行应为:

<skipOperationExample>false</skipOperationExample>

2. 添加SpringDoc兼容配置以生成@ExampleObject

在插件的configOptions中添加以下配置,开启SpringDoc注解生成和模型示例生成:

<configOptions>
    <sourceFolder>src/gen/java/main</sourceFolder>
    <delegatePattern>true</delegatePattern>
    <interfaceOnly>true</interfaceOnly>
    <!-- 新增配置 -->
    <useSpringDoc>true</useSpringDoc>
    <generateModelExamples>true</generateModelExamples>
</configOptions>
  • useSpringDoc: 开启后生成SpringDoc兼容的注解(包括@ExampleObject)
  • generateModelExamples: 强制生成模型类的示例注解

3. 验证openapi.json中的示例定义

确保你的openapi.json中正确定义了示例字段,比如请求体或响应体的example/examples:

"paths": {
  "/your-api": {
    "post": {
      "requestBody": {
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/YourModel" },
            "example": {
              "id": 1,
              "name": "示例名称",
              "description": "示例描述"
            }
          }
        }
      },
      "responses": {
        "200": {
          "description": "成功响应",
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/YourModel" },
              "example": {
                "id": 1,
                "name": "返回示例名称",
                "description": "返回示例描述"
              }
            }
          }
        }
      }
    }
  }
}

4. 版本兼容检查(可选)

如果上述配置调整后仍有问题,可以尝试升级依赖版本:

  • 将springdoc-openapi-ui升级到2.2.0
  • 将openapi-generator-maven-plugin升级到7.4.0
    新版本对注解生成的支持更完善,能减少兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 10:07:49