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

能否通过OpenAPI生成继承Spring HATEOAS RepresentationModel的实体?

如何用OpenAPI Generator生成继承Spring HATEOAS RepresentationModel的实体?

可以实现,主要通过自定义模板或插件配置结合OpenAPI扩展字段来完成,以下是具体方案:

方案一:自定义实体模板(灵活控制单/多实体继承)

OpenAPI Generator基于Mustache模板生成代码,你可以修改实体类模板来添加RepresentationModel继承逻辑:

  1. 复制对应版本(6.2.1)的Spring模板model.mustache到项目的src/main/resources/openapi-templates目录(无则创建)。
  2. 修改模板中的类声明部分,支持通过OpenAPI扩展字段控制是否继承:
    public class {{classname}} 
        {{#x-hateoas}}extends org.springframework.hateoas.RepresentationModel<{{classname}}> {{/x-hateoas}}
        {{#parent}}extends {{parent}}{{/parent}} 
        {{#interfaces}}implements {{.}}{{/interfaces}} {
    
  3. 在OpenAPI Spec的目标实体schema中添加扩展字段x-hateoas: true,例如:
    components:
      schemas:
        User:
          type: object
          x-hateoas: true
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
            # 其他字段定义
    
  4. 在Maven插件配置中指定自定义模板目录:
    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>6.2.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <generatorName>spring</generatorName>
                    <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                    <output>${project.build.directory}/generated-sources/openapi</output>
                    <!-- 指定自定义模板目录 -->
                    <templateDirectory>${project.basedir}/src/main/resources/openapi-templates</templateDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

方案二:全局配置实体父类(快速实现全实体继承)

如果所有实体都需要继承RepresentationModel,可以直接通过插件参数全局配置:

在Maven插件中添加modelParent参数,指定父类为RepresentationModel:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.2.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generatorName>spring</generatorName>
                <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                <output>${project.build.directory}/generated-sources/openapi</output>
                <!-- 全局设置实体父类 -->
                <modelParent>org.springframework.hateoas.RepresentationModel&lt;{classname}&gt;</modelParent>
            </configuration>
        </execution>
    </executions>
</plugin>

注:部分版本中{classname}占位符可能无法自动替换,此时建议结合方案一的模板修改实现泛型声明。

补充:添加JPA注解(对应示例中的实体需求)

如果需要生成带JPA注解的实体(如@Entity、@Id),可以在OpenAPI Spec中添加x-扩展字段,再修改模板解析这些字段:
比如在schema中定义:

components:
  schemas:
    User:
      type: object
      x-hateoas: true
      x-jpa-entity: true
      x-jpa-table: User
      properties:
        id:
          type: integer
          format: int64
          x-jpa-id: true
          x-jpa-generated-value: IDENTITY
        name:
          type: string
          x-jpa-column: name

然后修改模板添加对应的注解逻辑,例如:

{{#x-jpa-entity}}@Entity{{/x-jpa-entity}}
{{#x-jpa-table}}@Table(name = "{{x-jpa-table}}"){{/x-jpa-table}}
public class {{classname}} ... {
    {{#x-jpa-id}}@Id{{/x-jpa-id}}
    {{#x-jpa-generated-value}}@GeneratedValue(strategy = GenerationType.{{x-jpa-generated-value}}){{/x-jpa-generated-value}}
    private {{>datatype}} {{name}};
}

依赖验证

你已引入的spring-boot-starter-hateoas依赖已经包含RepresentationModel类,无需额外添加依赖;Spring Boot 3.0.0与openapi-generator-maven-plugin 6.2.1版本兼容,无需调整版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 07:40:22