Given an *existing* Jersey Jax-RS application, is there a way to generate OpenAPI 3.x JSON/Yaml?

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

Given an *existing* Jersey Jax-RS application, is there a way to generate OpenAPI 3.x JSON/Yaml?

问题

我有一个现有的Java应用程序,以前从未使用Swagger、OpenAPI等来创建API规范。因此,它们使用javax.ws.rs.*注解来指定应用程序的API契约。

我想使用Maven为每个项目生成Swagger或OpenAPI规范的JSON/YAML文件。

有人知道如何实现这个吗?

也许有一些Maven插件可以用来执行类似以下的操作:

mvn someplugin:generateOpenApiJsonUsingOnlyJaxRsAnnotations

输出将是target/generated-resources/openapi.json

我知道有swagger-maven-plugin插件... 但这不是相反的方向吗?意思是它接受OpenAPI的json/yaml并生成Java类?

有没有一种在编译时采用Jax-RS注解并生成OpenAPI的json/yaml的方法?

最重要的是,当我说“现有的Jax-RS”应用程序时... 这意味着我不能随意添加大量的swagger @Api注解。最好不要改变服务器端的部分,就能够从这些Jax-RS服务应用程序生成API客户端。

英文:

I have an existing Java application that previously has never used Swagger, OpenAPI, etc to create a API specification. So they have the javax.ws.rs.* annotations that specify the API contract for the application.

I'd like to generate a swagger or openapi specification JSON/YAML file for each project using maven.

Does anyone know how to accomplish this?

Is there maybe some Maven plugin you can use to do something like:

mvn someplugin:generateOpenApiJsonUsingOnlyJaxRsAnnotations

And the output would be target/generated-resources/openapi.json.

I know there is the swagger-maven-plugin plugin... but isn't that the reverse direction? Meaning it takes in a OpenAPI json/yaml and produces java classes?

Is there some way to go the other direction at compile time where you take the Jax-RS annotations and produce a openapi json/yaml?

Most importantly, when I say "Existing Jax-RS" application... that means I cannot go in and add a bunch of swagger @Api annotations all over the place. It would be the preference not to have to change the server side of the equation in order to generate the Api client from these Jax-RS service applications.

答案1

得分: 1

查看这个回答:

https://stackoverflow.com/questions/35475484/how-to-generate-swagger-spec-from-from-jax-rs-annotated-interface

它链接到这里:

https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5

所以基本上没有自动生成的方式。您必须在所有地方添加注释。

我真的希望我错了,因为不得不在旧代码中添加所有这些注释很麻烦。我永远不会得到任何人的支持来做这件事。

我创建了https://github.com/kongchen/swagger-maven-plugin/issues/894,提出了一个新的功能请求。

英文:

See this answer here

https://stackoverflow.com/questions/35475484/how-to-generate-swagger-spec-from-from-jax-rs-annotated-interface

Which links to here

https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5

So basically there is no way to automatically generate it. You have to put annotations on everything.

I would LOVE to be wrong about that, because it sucks having to add all these annotations to old code. I'd never get anyone's buy in do that.

I created https://github.com/kongchen/swagger-maven-plugin/issues/894 with a new feature request.

huangapple
  • 本文由 发表于 2023年7月31日 23:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804907.html
匿名

发表评论

匿名网友

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

确定