我目前正在将我们的OpenApi文档(Swagger1.5)迁移到Swagger2.0(Swagger3.0)。
API文档是使用maven包使用java注释生成的Swagger文档。
swagger-annotations和swagger-jaxrs..。我已经用新版本更新了pom.xml,所以它看起来像这样:
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?)
如果有人能告诉我正确的方向,我将不胜感激。
发布于 2019-01-16 19:29:46
经过一些研究后,我可以在他们的文档中找到一些关于它的文档
Github对于JAX-RS应用程序,因此结果与我之前所做的类似,但现在没有使用 BeanConfig,它使用OpenAPI和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);
}
}
}发布于 2019-05-01 14:49:05
尽管OP已经回答了他们自己的问题,但对于像我这样想要从swagger 1.x迁移到swagger 2.0 (openAPI 3)并需要完整配置的人,他们添加了更多细节。
(此示例适用于嵌入式jetty)
// 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在他们的答案中描述的内容来完成:
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);
}发布于 2021-03-01 17:40:15
对于上述需求,有一个简单得多的解决方案。
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.
https://stackoverflow.com/questions/54185836
复制相似问题