Swagger 2.0(OpenApi 3.0)中的BeanConfig(或类似的?) [英] BeanConfig (or similar?) in Swagger 2.0 (OpenApi 3.0)

查看:613
本文介绍了Swagger 2.0(OpenApi 3.0)中的BeanConfig(或类似的?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将我们的API文档(即Swagger 1.5)迁移到Swagger 2.0(OpenApi 3.0)

I am currently migrating our API docs (which were Swagger 1.5) to Swagger 2.0 (OpenApi 3.0)

API文档是Swagger文档,它们使用maven包swagger-annotationsswagger-jaxrs通过Java注释生成.我已经用新版本更新了pom.xml,因此它看起来像:

The API docs are Swagger docs which get generated with java annotations using maven packages swagger-annotations and swagger-jaxrs. I have already updated the pom.xml with new versions so it looks like:

        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-jaxrs2</artifactId>
            <version>2.0.6</version>
        </dependency>

并且所有旧注释都被新注释替换(变化很大)并且看起来不错.

And also all the old annotations are replaced with the new ones (which change quite a lot) and looks fine.

问题是,我们正在使用BeanConfig来定义文档的常规配置并自动扫描所有REST资源,以便在/swagger.json处自动生成文档.

The thing is we were using a BeanConfig to define the docs general config and auto-scan all the REST resources so the documentation got generated automatically at /swagger.json.

问题是我找不到创建BeanConfig并自动扫描资源的新方法",因此一切都在/swagger.json/openapi.json(也许现在像OpenAPIDefinition一样?)

The problem is I can't find the "new way" of doing such thing as creating a BeanConfig and auto-scan the resources so everything gets generated at /swagger.json or /openapi.json (maybe now is something like OpenAPIDefinition?)

如果有人能指出我正确的方向,我将不胜感激...

If somebody could point me to the right direction I would be very grateful...

推荐答案

经过研究,我可以在他们的

After some research, I could find some documentation about it in their Github for JAX-RS application, so the result is something similar to what I was doing but now instead of using a BeanConfig, it uses OpenAPI and Info:

@ApplicationPath("/sample")
public class MyApplication extends Application {

    public MyApplication(@Context ServletConfig servletConfig) {
        super();
        OpenAPI oas = new OpenAPI();
        Info info = new Info()
            .title("Swagger Sample App bootstrap code")
            .description("This is a sample server Petstore server.  You can find out more about Swagger " +
                    "at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, " +
                    "you can use the api key `special-key` to test the authorization filters.")
            .termsOfService("http://swagger.io/terms/")
            .contact(new Contact()
                    .email("apiteam@swagger.io"))
            .license(new License()
                    .name("Apache 2.0")
                    .url("http://www.apache.org/licenses/LICENSE-2.0.html"));

        oas.info(info);
        SwaggerConfiguration oasConfig = new SwaggerConfiguration()
            .openAPI(oas)
            .prettyPrint(true)
            .resourcePackages(Stream.of("io.swagger.sample.resource").collect(Collectors.toSet()));

        try {
            new JaxrsOpenApiContextBuilder()
                .servletConfig(servletConfig)
                .application(this)
                .openApiConfiguration(oasConfig)
                .buildContext(true);
        } catch (OpenApiConfigurationException e) {
            throw new RuntimeException(e.getMessage(), e);
        }

    }
}

这篇关于Swagger 2.0(OpenApi 3.0)中的BeanConfig(或类似的?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆