Swagger 2.0 (OpenApi 3.0) 中的 BeanConfig(或类似的?)

2022-01-22 00:00:00 migration swagger java openapi

我目前正在将我们的 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 文档是使用 maven 包 swagger-annotationsswagger-jaxrs 生成的 Swagger 文档.我已经用新版本更新了 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...

推荐答案

经过一番研究,我可以在他们的 Github 用于 JAX-RS 应用程序,所以结果类似于我正在做,但现在不是使用 BeanConfig,而是使用 OpenAPIInfo:

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

    }
}

相关文章