OpenAPI等效于Jersey Jax-RS com.github.kongchen:swagger-maven-plugin?

huangapple go评论147阅读模式
英文:

OpenAPI equivilant of Jersey Jax-RS com.github.kongchen:swagger-maven-plugin?

问题

在Swagger中,你可以使用swagger-maven-plugin在编译时构建合同的.json文件。

  1. <plugin>
  2. <groupId>com.github.kongchen</groupId>
  3. <artifactId>swagger-maven-plugin</artifactId>
  4. <version>3.1.3</version>
  5. <configuration>
  6. <apiSources>
  7. <apiSource>
  8. <springmvc>false</springmvc>
  9. <locations>com.example.rest.resources</locations>
  10. <schemes>https</schemes>
  11. <info>
  12. <title>project Service</title>
  13. <version>v2</version>
  14. <description>Description here.</description>
  15. <contact>
  16. <email>test@example.com</email>
  17. <name>project Service</name>
  18. </contact>
  19. </info>
  20. <outputPath>${basedir}/target/generated-resources/document.html</outputPath>
  21. <swaggerDirectory>${basedir}/target/generated-resources</swaggerDirectory>
  22. </apiSource>
  23. </apiSources>
  24. </configuration>
  25. <executions>
  26. <execution>
  27. <phase>compile</phase>
  28. <goals>
  29. <goal>generate</goal>
  30. </goals>
  31. </execution>
  32. </executions>
  33. </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.

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;com.github.kongchen&lt;/groupId&gt;
  3. &lt;artifactId&gt;swagger-maven-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;3.1.3&lt;/version&gt;
  5. &lt;configuration&gt;
  6. &lt;apiSources&gt;
  7. &lt;apiSource&gt;
  8. &lt;springmvc&gt;false&lt;/springmvc&gt;
  9. &lt;locations&gt;com.example.rest.resources&lt;/locations&gt;
  10. &lt;schemes&gt;https&lt;/schemes&gt;
  11. &lt;info&gt;
  12. &lt;title&gt;project Service&lt;/title&gt;
  13. &lt;version&gt;v2&lt;/version&gt;
  14. &lt;description&gt;Description here.&lt;/description&gt;
  15. &lt;contact&gt;
  16. &lt;email&gt;test@example.com&lt;/email&gt;
  17. &lt;name&gt;project Service&lt;/name&gt;
  18. &lt;/contact&gt;
  19. &lt;/info&gt;
  20. &lt;outputPath&gt;${basedir}/target/generated-resources/document.html&lt;/outputPath&gt;
  21. &lt;swaggerDirectory&gt;${basedir}/target/generated-resources&lt;/swaggerDirectory&gt;
  22. &lt;/apiSource&gt;
  23. &lt;/apiSources&gt;
  24. &lt;/configuration&gt;
  25. &lt;executions&gt;
  26. &lt;execution&gt;
  27. &lt;phase&gt;compile&lt;/phase&gt;
  28. &lt;goals&gt;
  29. &lt;goal&gt;generate&lt;/goal&gt;
  30. &lt;/goals&gt;
  31. &lt;/execution&gt;
  32. &lt;/executions&gt;
  33. &lt;/plugin&gt;

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的示例:

  1. OpenApiContext ctx = new JaxrsOpenApiContextBuilder()
  2. .application(new YourResourceConfigurationWithOpenApi())
  3. .buildContext(true);
  4. OpenAPI oas = ctx.read();
  5. String res = ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas);
  6. 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:

  1. OpenApiContext ctx = new JaxrsOpenApiContextBuilder()
  2. .application(new YourResourceConfigurationWithOpenApi())
  3. .buildContext(true);
  4. OpenAPI oas = ctx.read();
  5. String res = ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas);
  6. System.out.println(res);

huangapple
  • 本文由 发表于 2023年8月4日 01:51:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定