我已成功从 .yaml open-api 描述符文件生成接口,但是,如问题标题所示,我希望将这些接口的响应类型从 ResponseEntity 更改为我自己的类型。基本上而不是具有此签名的接口:
ResponseEntity<Void> clearCache();
对于基本上以这种方式实现的方法:
public void clearCache(){ //do something}
我希望生成的界面就像
void clearCache();
对于我自己定义的类也是如此,而不是ResponseEntity<MyBook> getBook(String ISBN);
我希望它只是使用MyBook
作为返回类型,所以它应该看起来像MyBook getBook(String ISBN);
我用于 openapi-generator 插件的当前设置是
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>my-own-service-be/src/main/resources/api-docs.yaml</inputSpec>
<generatorName>spring</generatorName>
<additionalProperties>
<additionalProperty>skipDefaultInterface=true</additionalProperty>
<additionalProperty>interfaceOnly=true</additionalProperty>
</additionalProperties>
<generateApis>true</generateApis>
<apiPackage>controller</apiPackage>
<supportingFilesToGenerate>false</supportingFilesToGenerate>
<modelPackage>dto</modelPackage>
<generateModelTests>false</generateModelTests>
<generateApiTests>false</generateApiTests>
</configuration>
</execution>
</executions>
</plugin>
我们最近遇到了类似的挑战。您需要做的就是调整模板。为此,您需要从 OpenAPI 项目中找到生成器的源模板。那将是this api.mustache file在你的情况下。
只需将其复制到例如你的src/main/resources/
文件夹(可能位于名为custom
)并根据您的需要进行调整,即替换响应类型。
然后你需要调整你的pom.xml
这样您的自定义模板文件就会被实际使用:
<configuration>
<!-- The following line is crucial: -->
<templateDirectory>${project.basedir}/src/main/resources/custom</templateDirectory>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>spring</generatorName>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
还可以看看这个模板文档有关该主题的更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)