2

How to set overriding models in swagger-maven-plugin 3.1.0 and Swagger UI 2.0 (or newer versions)?

Recently we've upgraded Swagger UI from 1.2 to 2.0 and swagger-maven-plugin from 2.3 to 3.1.0.

It appears, that swagger-maven-plugin version 3.1.0 is missing the overridingModels option, that was present in version 2.3.

The option enabled us to customize schema description for certain data types, as described in: https://github.com/swagger-api/swagger-core/wiki/overriding-models.

4

2 回答 2

2

以下是我最终解决问题的方法(我使用的是 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.xmlswagger-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 模块中,在 directory 中src/main/resources
注意:文件的名称是任意的,但它应该与 pom.xml 中的值匹配。

于 2016-09-12T16:08:44.580 回答
0

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

于 2021-09-03T02:19:28.207 回答