gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:37:41 26 4
gpt4 key购买 nike

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

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

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

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

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

问题 是我找不到执行诸如创建 BeanConfig 和自动扫描资源这样的事情的“新方法”,因此所有内容都在 /swagger.json/openapi.json(也许现在类似于 OpenAPIDefinition?)

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

最佳答案

经过一些研究,我可以在他们的 Github 中找到一些关于它的文档对于 JAX-RS 应用程序,所以结果与我所做的类似,但现在不是使用 BeanConfig,而是使用 OpenAPIInfo:

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

}
}

关于java - Swagger 2.0 (OpenApi 3.0) 中的 BeanConfig(或类似的?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54185836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com