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

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

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.

&lt;plugin&gt;
	&lt;groupId&gt;com.github.kongchen&lt;/groupId&gt;
	&lt;artifactId&gt;swagger-maven-plugin&lt;/artifactId&gt;
	&lt;version&gt;3.1.3&lt;/version&gt;
	&lt;configuration&gt;
		&lt;apiSources&gt;
			&lt;apiSource&gt;
				&lt;springmvc&gt;false&lt;/springmvc&gt;
				&lt;locations&gt;com.example.rest.resources&lt;/locations&gt;
				&lt;schemes&gt;https&lt;/schemes&gt;
				&lt;info&gt;
					&lt;title&gt;project Service&lt;/title&gt;
					&lt;version&gt;v2&lt;/version&gt;
					&lt;description&gt;Description here.&lt;/description&gt;
					&lt;contact&gt;
						&lt;email&gt;test@example.com&lt;/email&gt;
						&lt;name&gt;project Service&lt;/name&gt;
					&lt;/contact&gt;
				&lt;/info&gt;
				&lt;outputPath&gt;${basedir}/target/generated-resources/document.html&lt;/outputPath&gt;
				&lt;swaggerDirectory&gt;${basedir}/target/generated-resources&lt;/swaggerDirectory&gt;
			&lt;/apiSource&gt;
		&lt;/apiSources&gt;
	&lt;/configuration&gt;
	&lt;executions&gt;
		&lt;execution&gt;
			&lt;phase&gt;compile&lt;/phase&gt;
			&lt;goals&gt;
				&lt;goal&gt;generate&lt;/goal&gt;
			&lt;/goals&gt;
		&lt;/execution&gt;
	&lt;/executions&gt;
&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的示例:

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

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:

确定