java 如何从 JAX RS 注释接口生成 swagger 规范?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35475484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:10:29  来源:igfitidea点击:

How to generate swagger spec from from JAX RS annotated interface?

javamavenswagger

提问by mac

I was wondering if there's a way to generate the swagger spec from an existing JAX RS annotated interface, e.g. something like this:

我想知道是否有办法从现有的 JAX RS 注释接口生成 swagger 规范,例如这样的:

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface ISampleService {

    @GET
    @Path("sample/foobar/{foobarId}")
    Foobar getFoobar(@PathParam("foobarId") String foobarId);

    @PUT
    @Path("sample/foobar/{foobarId}")
    Foobar updateFoobar(@PathParam("foobarId") String foobarId, Foobar foobar);
}

I did find this "jaxrs analyzer" project, but it doesn't seem to recognize my interface.

我确实找到了这个“jaxrs 分析器”项目,但它似乎无法识别我的界面。

Basically, I have my service defined in an interface like that, and I want to reverse-engineer the swagger spec to be able to post it so anyone else can generate (other) clients using the swagger functionality.

基本上,我在这样的接口中定义了我的服务,我想对 swagger 规范进行逆向工程,以便能够发布它,以便其他任何人都可以使用 swagger 功能生成(其他)客户端。

Preferably, the solution would just work in a maven build (plugin).

最好,该解决方案仅适用于 maven 构建(插件)。

回答by Paul

Swagger has an easy-to-follow tutorial for Jersey here.

Swagger在此处为 Jersey 提供了一个易于遵循的教程。

In order for Swagger to generate a specification for your resources, you need to add the @Apiannotation at the top of your class. The available annotations are listed here, but I just use @Api.

为了让 Swagger 为您的资源生成规范,您需要@Api在类的顶部添加注释。此处列出可用的注释,但我只使用@Api.