首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BeanConfig (或类似的?)在Swager2.0 (OpenApi 3.0)中

BeanConfig (或类似的?)在Swager2.0 (OpenApi 3.0)中
EN

Stack Overflow用户
提问于 2019-01-15 00:50:58
回答 3查看 5.9K关注 0票数 7

我目前正在将我们的OpenApi文档(Swagger1.5)迁移到Swagger2.0(Swagger3.0)。

API文档是使用maven包使用java注释生成的Swagger文档。

swagger-annotationsswagger-jaxrs..。我已经用新版本更新了pom.xml,所以它看起来像这样:

代码语言:javascript
复制
io.swagger.core.v3
            swagger-annotations
            2.0.6
        
        
            io.swagger.core.v3
            swagger-jaxrs2
            2.0.6

而且所有旧的注解都被新的注解替换(变化很大),看起来很好。

问题是我们使用了一个BeanConfig

要定义文档的一般配置并自动扫描所有REST资源,以便在以下位置自动生成文档/swagger.json..。

The The The问题我找不到一种“新方法”来做这样的事情,比如创建一个BeanConfig并自动扫描资源,这样所有的东西都会在

/swagger.json或者/openapi.json

(也许现在有点像OpenAPIDefinition?)

如果有人能告诉我正确的方向,我将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-16 19:29:46

经过一些研究后,我可以在他们的文档中找到一些关于它的文档

Github对于JAX-RS应用程序,因此结果与我之前所做的类似,但现在没有使用 BeanConfig,它使用OpenAPIInfo

代码语言:javascript
复制
@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);
        }

    }
}
票数 9
EN

Stack Overflow用户

发布于 2019-05-01 14:49:05

尽管OP已经回答了他们自己的问题,但对于像我这样想要从swagger 1.x迁移到swagger 2.0 (openAPI 3)并需要完整配置的人,他们添加了更多细节。

(此示例适用于嵌入式jetty)

代码语言:javascript
复制
// Jetty configuration

// ContextHandlerCollection contexts

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/api");
context.addFilter(GzipFilter.class, "/*", EnumSet.allOf(DispatcherType.class));

ResourceConfig resourceConfig = new ResourceConfig(ImmutableSet.>builder()
                                                                .add(MyRestService.class)
                                                                .build());
resourceConfig.registerClasses(OpenApiResource.class,AcceptHeaderOpenApiResource.class); // for swagger, this will cerate openapi.json at /api/openapi.json
context.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
contexts.addHandler(context);

如果您需要更改默认的swagger配置,可以通过OP在他们的答案中描述的内容来完成:

代码语言:javascript
复制
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);
        }
票数 3
EN

Stack Overflow用户

发布于 2021-03-01 17:40:15

对于上述需求,有一个简单得多的解决方案。

代码语言:javascript
复制
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import org.glassfish.jersey.server.ResourceConfig;


@OpenAPIDefinition(
    info =
        @Info(
            title = "Sample rest service",
            version = "1.0.0",
            description = "Sample rest service",
            contact =
                @Contact(
                    url = "https://jira2.cerner.com/projects/Dey",
                    name = "ADey")))
public class SampleRestApplication extends ResourceConfig {
  public SampleRestApplication() {
    register(OpenApiResource.class);
  }
}

您的服务将在以下位置加载您的API规范/openApi.yaml|json.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54185836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档