英文:
OpenAPI equivilant of Jersey Jax-RS com.github.kongchen:swagger-maven-plugin?
问题
在Swagger中,你可以使用swagger-maven-plugin
在编译时构建合同的.json文件。
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.3</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>false</springmvc>
<locations>com.example.rest.resources</locations>
<schemes>https</schemes>
<info>
<title>project Service</title>
<version>v2</version>
<description>Description here.</description>
<contact>
<email>test@example.com</email>
<name>project Service</name>
</contact>
</info>
<outputPath>${basedir}/target/generated-resources/document.html</outputPath>
<swaggerDirectory>${basedir}/target/generated-resources</swaggerDirectory>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
在给定的Jax-RS Maven项目中,是否有一种等效的方式可以使用插件在Maven构建时创建一个OpenAPI客户端,使用当前项目的Java源代码生成存根?
我找到了一个项目,它在集成测试阶段实际上启动应用程序使用Spring测试运行器,然后使用HTTP端点构建合同。
我猜它可以工作,但不是很方便。Swagger的方法在编译时静态运行要好得多。
英文:
In Swagger, you could use the swagger-maven-plugin
to build the contract .json at compile time.
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.3</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>false</springmvc>
<locations>com.example.rest.resources</locations>
<schemes>https</schemes>
<info>
<title>project Service</title>
<version>v2</version>
<description>Description here.</description>
<contact>
<email>test@example.com</email>
<name>project Service</name>
</contact>
</info>
<outputPath>${basedir}/target/generated-resources/document.html</outputPath>
<swaggerDirectory>${basedir}/target/generated-resources</swaggerDirectory>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Is there an equivalent way to Given a Jax-RS maven project, use a plugin create an OpenAPI client at maven build time using the current project's java source to generate a stubs?
I found a project out there that uses the integration-test phase to actually start the app using spring test runner then uses the http endpoint to build the contract.
I guess it works but it's not very convenient. The swagger one was way better it ran statically at compile time.
答案1
得分: 0
请查看 https://github.com/springdoc/springdoc-openapi/issues/140
查看 springdoc-openapi-maven-plugin,它可以帮助您在Maven的集成测试阶段生成yml/json OpenAPI描述。
https://github.com/springdoc/springdoc-openapi-maven-plugin
请注意,这不是在编译时完成的,而是在集成测试阶段进行,当您有一个正在运行的Spring服务器可用时。
目前似乎还没有我能找到的用于此目的的Maven插件。
您可以在Java中非常简单地完成这个任务。
假设您有一个Jax-RS应用程序:YourResourceConfigurationWithOpenApi
,下面是一个在没有运行Spring引导应用程序的情况下编写yaml的示例:
OpenApiContext ctx = new JaxrsOpenApiContextBuilder()
.application(new YourResourceConfigurationWithOpenApi())
.buildContext(true);
OpenAPI oas = ctx.read();
String res = ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas);
System.out.println(res);
英文:
See https://github.com/springdoc/springdoc-openapi/issues/140
Look at springdoc-openapi-maven-plugin which can help you generate the yml/json OpenAPI description during integeration test phase in maven.
https://github.com/springdoc/springdoc-openapi-maven-plugin
Note this does not do it at compile time. But it does it in the integration test phase when you have a running Spring server available.
Does not seem to be a Maven plugin for this yet that I can find.
You can very simply do this in Java.
Assuming you have a Jax-RS application: YourResourceConfigurationWithOpenApi
here is an example of how to write yaml without Spring boot application running:
OpenApiContext ctx = new JaxrsOpenApiContextBuilder()
.application(new YourResourceConfigurationWithOpenApi())
.buildContext(true);
OpenAPI oas = ctx.read();
String res = ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas);
System.out.println(res);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论