我花了几个小时研究如何在Java中使用swagger codegen生成OAS spec yaml文件,但我放弃了。我想提供Java源代码中的所有API规范数据作为代码注释。如果能通过maven将其公开,那就太好了。
我应该使用swagger-codegen-maven-plugin,但我不能让它扫描源代码来生成OAS yaml或JSON文件。
我会感谢一个有效的codegen插件配置的pom.xml片段。
也许我应该回到前面的Swagger,因为这个用例是在2.x中直接处理的。现在我对3.x的方法感到沮丧。
发布于 2019-09-11 04:18:12
Swagger Codegen从OpenAPI文件生成代码。要做相反的事情--从Java代码注释生成OpenAPI文件--你需要Swagger Core,比如它的Maven插件,swagger-maven-plugin。
将以下依赖项添加到pom.xml
<dependencies>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>然后在您的构建中使用它;示例配置:
<plugins>
<plugin>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>2.0.9</version>
<configuration>
<outputFileName>openapi</outputFileName>
<outputPath>${project.build.directory}/generatedtest</outputPath>
<configurationFilePath>${project.basedir}/src/main/resources/configurationFile.yaml</configurationFilePath>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>resolve</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>https://stackoverflow.com/questions/57877466
复制相似问题