更改Spring Openapi-Generator-maven-plugin生成的接口的返回类型 [英] Change return types for Spring openapi-generator-maven-plugin generated interfaces

查看:60
本文介绍了更改Spring Openapi-Generator-maven-plugin生成的接口的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法从.yaml开放API描述符文件生成了接口,但是,正如问题标题中所述,我希望将这些接口的响应类型从ResponseEntity更改为我自己的类型。基本上不是具有以下签名的接口:

 ResponseEntity<Void> clearCache();

对于基本上是这样实现的方法:

public void clearCache(){ //do something}

我希望生成的接口与

一样简单
void clearCache();
我自己定义的类也是这样,而不是ResponseEntity<MyBook> getBook(String ISBN);,我希望它只使用MyBook作为返回类型,因此它应该类似于 我当前对Openapi生成器插件使用的设置是

        <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>

有关该主题的更多信息,请参阅this templating documentation

这篇关于更改Spring Openapi-Generator-maven-plugin生成的接口的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆