作为一名新手,我试图将swagger-core与我的其他api整合在一起 - > https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5
面对BeanConfig的setScan功能的一些问题所以降级反射包,可以从下面的maven文件中看到:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.18</version>
<exclusions>
<exclusion>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.13.6</version>
<scope>runtime</scope>
</dependency>
这是应用程序启动方法:
public void start() throws Exception {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("2.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("0.0.0.0:8082");
beanConfig.setBasePath("/test");
beanConfig.setResourcePackage("io.confluent.kafkarest.resources");
beanConfig.setScan(true);
beanConfig.setPrettyPrint("true");
if (server == null) {
createServer();
}
server.start();
}
通过这个我能够在根路径生成swagger.json。 现在,我想把这个json提供给swagger ui,所以我在maven pom.xml中增加了依赖性,如上所示。并修改了如下代码:
public void start() throws Exception {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("2.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("0.0.0.0:8082");
beanConfig.setBasePath("/test");
beanConfig.setResourcePackage("io.confluent.kafkarest.resources");
beanConfig.setScan(true);
beanConfig.setPrettyPrint("true");
if (server == null) {
createServer();
}
server.setHandler(buildSwaggerUI()); //--> added this method call.
server.start();
}
public static ContextHandler buildSwaggerUI() throws Exception {
ResourceHandler rh = new ResourceHandler();
rh.setResourceBase(KafkaRestApplication.class.getClassLoader()
.getResource("META-INF/resources/webjars/swagger-ui/3.13.6")
.toURI().toString());
ContextHandler context = new ContextHandler();
context.setContextPath("/swagger");
context.setHandler(rh);
return context;
}
在此之后,我能够获得相当默认的swagger ui,但现在swagger.json已经消失了。我不再是swagger.json了。我试着关注这个博客: https://danielflower.github.io/2016/04/01/Swagger-with-embedded-jetty-without-magic.html
我真的很想让swagger ui与swagger json spec一起工作。我觉得我错过了一些傻事?请帮忙。
答案 0 :(得分:0)
我按照this链接开始工作。我的工作代码看起来像这样:
public void start() throws Exception {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("2.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("0.0.0.0:8082");
beanConfig.setBasePath("/test");
beanConfig.setResourcePackage("io.confluent.kafkarest.resources");
beanConfig.setScan(true);
beanConfig.setPrettyPrint("true");
if (server == null) {
createServer();
}
final HandlerList handlers = new HandlerList();
handlers.addHandler(buildSwaggerUI());
handlers.addHandler(buildContext());
server.setHandler(handlers);
server.start();
}
private static ContextHandler buildContext() {
ResourceConfig resourceConfig = new ResourceConfig().packages(WS_PACKAGE, "io.swagger.jaxrs.listing");
ServletContainer servletContainer = new ServletContainer(resourceConfig);
ServletHolder eBrowser = new ServletHolder(servletContainer);
ServletContextHandler eContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
eContext.setContextPath("/");
eContext.addServlet(eBrowser, "/*");
return eContext;
}
public static ContextHandler buildSwaggerUI() throws Exception {
ResourceHandler rh = new ResourceHandler();
rh.setResourceBase(KafkaRestApplication.class.getClassLoader()
.getResource("META-INF/resources/webjars/swagger-ui/3.13.6")
.toURI().toString());
ContextHandler context = new ContextHandler();
context.setContextPath("/swagger");
context.setHandler(rh);
return context;
}