【问题标题】:How to set overriding models in swagger-maven-plugin 3.1.0如何在 swagger-maven-plugin 3.1.0 中设置覆盖模型
【发布时间】:2015-10-06 23:57:57
【问题描述】:

如何在 swagger-maven-plugin 3.1.0 和 Swagger UI 2.0(或更新版本)中设置覆盖模型?

最近我们将 Swagger UI 从 1.2 升级到 2.0,并将 swagger-maven-plugin 从 2.3 升级到 3.1.0。

看来,swagger-maven-plugin 版本 3.1.0 缺少 overridingModels 选项,存在于 2.3 版中。

该选项使我们能够为某些数据类型自定义架构描述,如:https://github.com/swagger-api/swagger-core/wiki/overriding-models 中所述。

【问题讨论】:

    标签: maven swagger swagger-ui swagger-2.0 swagger-maven-plugin


    【解决方案1】:

    这是我最终解决问题的方法(我使用的是 swagger-maven-plugin 版本 3.1.1):

    假设您想将java.util.Date 映射为这个任意结构:

    {
      "year": "string",
      "month": "string",
      "day": "string"
    }
    

    第 1 步:创建一个描述上述模型的类:

    package com.example;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    
    @ApiModel
    public class MyDate {
    
        @ApiModelProperty
        public String year;
    
        @ApiModelProperty
        public String month;
    
        @ApiModelProperty
        public String day;
    }
    

    注意:类名和包名是任意的,它们只需要在类路径上,因此类对插件可见,就像模型的其余部分一样。

    第2步:在pom.xml中将这一行添加到swagger-maven-plugin的配置中:

    <plugin>
      <groupId>com.github.kongchen</groupId>
      <artifactId>swagger-maven-plugin</artifactId>
      <version>3.1.1</version>
      <configuration>
        <apiSources>
          <apiSource>
            ...
            <modelSubstitute>/model-substitute.csv</modelSubstitute>
          </apiSource>
        </apiSources>
      </configuration>
      ...
    </plugin>
    

    第 3 步:使用以下映射创建文件 model-substitute.csv

    java.util.Date : com.example.MyDate
    

    将文件放在与插件相同的maven模块中,在目录src/main/resources中。
    注意:文件名是任意的,但要与 pom.xml 中的值匹配。

    【讨论】:

    • 在这种情况下,我只是得到一个空指针:无法在项目 xxx 上执行目标 com.github.kongchen:swagger-maven-plugin:3.1.3:generate (default): null: MojoExecutionException: NullPointerException
    • @spyro 使用 -X 选项运行 mvn 以查看完整的堆栈跟踪。它可能有助于解决您的问题。
    【解决方案2】:

    NullPointerException 可能是因为您没有 src/main/resources 中的文件。这在 3.1.7 版本中对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2020-04-05
      • 2016-10-02
      • 2015-09-12
      • 2021-03-31
      • 2021-07-24
      • 1970-01-01
      相关资源
      最近更新 更多