我如何才能获得敏捷的代码生成器,以使用现有的类而不是创建新的类?这可能吗?例如,我想使用org.springframework.data.domain.Page
而不是大张旗鼓地创建另一个页面类。
最佳答案
您可以使用--import-mappings
,因为它解释了here:
Sometimes you don't want a model generated. In this case, you can simply specify an import mapping to tell the codegen what not to create. When doing this, every location that references a specific model will refer back to your classes.
您可以在
swagger-codegen-cli generate
上调用它,包括的示例将是--import-mappings Page=org.springframework.data.domain.Page
尽管在常规配置参数here中未包含
importMappings
,但是如果您查看代码here,则可以看到它是List<String>
。我还没有将其与Maven插件一起使用,而是查看文档和代码,我猜这应该可以工作:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2-SNAPSHOT</version>
<executions>
<execution>
...
<configuration>
...
<importMappings>
<importMapping>Page=org.springframework.data.domain.Page</importMapping>
</importMappings>
</configuration>
</execution>
</executions>
</plugin>
但这是recently changed,因此如果您使用的是旧版插件,则可能会有所不同。在那之前改变似乎是这样的:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2-SNAPSHOT</version>
<executions>
<execution>
...
<configuration>
...
<configOptions>
<import-mappings>Page=org.springframework.data.domain.Page;Some=org.example.Some</import-mappings>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
根据该提交中的评论,也应该支持旧版本,但是我没有尝试过任何版本,因此请让我知道它是否有效。
关于swagger-codegen - Swagger Codegen使用现有类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612080/