这是我最终解决问题的方法(我使用的是 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 中的值匹配。