我已经设法从一个.yaml开放api描述符文件生成了这些接口,但是,正如问题标题中所述,我希望将这些接口的响应类型从ResponseEntity更改为我自己的类型。基本上,不是具有此签名的接口:
ResponseEntity<Void> clearCache();对于基本上以这种方式实现的方法:
public void clearCache(){ //do something}我希望生成的接口像
void clearCache();对于我自己定义的类也是如此,我希望它只使用MyBook作为返回类型,而不是ResponseEntity<MyBook> getBook(String ISBN);,所以它看起来应该类似于MyBook getBook(String ISBN);。我当前用于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>发布于 2020-05-02 23:42:49
我们最近遇到了类似的挑战。您需要做的是调整模板。为此,您需要从您的生成器的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。
发布于 2021-02-25 22:45:21
我不明白你为什么要这么做。您可以将ResponseEntity简单地想象为您的类型的包装器,添加处理标准HTTP功能的可能性,如HTTP状态代码和标头。有关更多详细信息,请参阅here。在你的情况下,你只会做
ResponseEntity.noContent().build(); //void
ResponseEntity.ok(myBook) //to return your MyBook type发布于 2021-06-20 19:38:27
Answer by philonous是向好的方向迈出的一步。所有Spring Boot的模板都可以在here中找到。但仅更改"api.mustache“是不够的,因为"api.moustache”不包含用于默认方法实现的模板。您必须禁用默认方法实现的生成,或者更改它的模板。
因此,如前所述,您必须在pom.xml中指定模板目录:
<configuration>
<templateDirectory>${project.basedir}/src/main/resources/custom</templateDirectory>
<skipDefaultInterface>true</skipDefaultInterface>步骤1- api.mustache
这里删除"import org.springframework.http.ResponseEntity;",只是为了确保它不再被使用。然后,通过将“ResponseEntity<{{> return }”替换为“{{>return}}”,从返回类型中删除ResponseEntity。问题是,这仍然会生成默认的实现“在生成的Api类中返回新的ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);”,所以我们必须更改其他模板。
第2步-禁用默认方法生成或更改methodBody.mustache
使用"skipDefaultInterface“选项禁用默认实现的生成(如pom.xml中所示)。否则,编辑"methodBody.mustache“,在"return new ResponseEntity<>”前面添加"throw new IllegalArgumentException("Not implemented");//“,或者做一些其他事情来表明方法还没有实现。
https://stackoverflow.com/questions/61476389
复制相似问题