首页 > 解决方案 > 使用实现外部接口的 openapi 生成器创建模型类

问题描述

我正在使用 openapi-generator 生成 java 类。

我希望模型类实现一个尚未由 openapi-generator 生成的外部接口。

是否有可以在模型 yaml 中定义的东西或可以传递给允许这种行为的 openapi-generator-maven-plugin 的属性?

所需行为示例:

package com.example.model;

/**
 * ExampleModel
 */
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class ExampleModel implements com.example.CustomInterface {
  @JsonProperty("property1")
  private String property1;

  @JsonProperty("property2")
  private String property2;

标签: javaopenapiopenapi-generator

解决方案


如果您想以相同的方式修改所有类,我会选择更改模板。在你的情况下,这很可能是这个文件:pojo.mustache

只需将其复制到您的src/main/resources/文件夹(可能在名为 custom 的子文件夹中)并根据您的需要进行调整。

然后你也需要调整你的pom.xml

<configuration>

    <!-- The following line is crucial: -->
    <templateDirectory>${project.basedir}/src/main/resources/custom</templateDirectory>

    <!-- Your other config goes here: -->
    <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
    <generatorName>java</generatorName>
    <configOptions>
        <sourceFolder>src/gen/java/main</sourceFolder>
    </configOptions>
</configuration>

另请查看此模板文档以获取有关该主题的更多信息。


推荐阅读