我正在使用 OpenApi 生成器来创建服务器代码 (jaxrs-jersey)。 我想阻止生成某些类。
在我的具体情况下,对于每个 api,我只想生成一些类。
我知道您可以创建一个自定义生成器,但默认 jaxrs-jersey 生成器生成的大多数代码对我来说都很好,所以我宁愿继续使用默认生成器。
有办法实现吗?
谢谢。
最佳答案
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. Note, this may not apply to all languages...
To specify an import mapping, use the --import-mappings argument and specify the model-to-import logic as such:
--import-mappings Pet=my.models.MyPet
Or for multiple mappings:
--import-mappings Pet=my.models.MyPet,Order=my.models.MyOrder
or
--import-mappings Pet=my.models.MyPet --import-mappings Order=my.models.MyOrder
我在 Maven 插件中应用了上面的内容,如下所示:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.3.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<importMappings>Comment=com.example.project.Comment</importMappings>
<generatorName>java</generatorName>
<library>resttemplate</library>
</configuration>
</execution>
</executions>
</plugin>
因此,在我的示例中,我有一个名为“Comment”的类,它是由 openapi 生成器错误地自动生成的。因此,我自己创建了 Comment 类,并将其放入 importMappings 字段中的上述路径“com.example.Comment”中。我将它作为参数传递到 importMappings 字段中,就像上面一样。经过上述设置后,Comment 类不再由 openapi-generator 生成,并且我的 Comment 类也不会被覆盖
关于openapi-generator - 如何阻止 OpenApi 生成器生成某些类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67998068/