java – 使用带Swagger的多个JAX-RS应用程序类
作者:互联网
我正在尝试在Java应用程序上实现Swagger,该应用程序有两个Application类,因为一个处理“公共”Web服务,另一个处理“管理”Web服务.我正在尝试生成两个单独的swagger.json文件,每个Application类一个.但是,两个URL中只生成其中一个.这是一些代码:
公共应用类:
@WebServlet
@ApplicationPath("/public")
public class PublicApplication extends Application {
public PublicApplication() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setTitle("A Fine Title");
beanConfig.setDescription("A Fine Description.");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setBasePath("/api");
beanConfig.setResourcePackage("com.test.rest.resource.external");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
私人申请类:
@WebServlet
@ApplicationPath("/admin")
public class AdminApplication extends Application {
public AdminApplication() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setTitle("Another Fine Title");
beanConfig.setDescription("Another Fine Description.");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setBasePath("/apiTwo");
beanConfig.setResourcePackage("com.test.rest.resource.internal");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
现在,如果我点击这些网址中的任何一个,我会得到相同的“公共”swagger json文件:
> http://localhost:9081/myApp/public/swagger.json
> http://localhost:9081/myApp/admin/swagger.json
我究竟做错了什么?
感谢所有阅读的人!
解决方法:
默认情况下,Swagger会对扫描仪和配置进行一次初始化.如果您有多个应用程序或配置,则需要设置configId,scannerId& contextId到您的每个应用程序通过BeanConfig,这应该与您的servlet配置中的值匹配.这个设置只适用于最新版本的swagger我认为.我试着用swagger-1.5.13.一个例子如下所示.
public class PublicApplication extends Application {
public PublicApplication() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setTitle("A Fine Title");
beanConfig.setDescription("A Fine Description.");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setBasePath("/api");
beanConfig.setResourcePackage("com.test.rest.resource.external");
beanConfig.setPrettyPrint(true);
// Set configId,contextId & scannerId
beanConfig.setConfigId("public");
beanConfig.setContextId("public");
beanConfig.setScannerId("public");
beanConfig.setScan(true);
}
您的AdminApplication类
public AdminApplication() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setTitle("Another Fine Title");
beanConfig.setDescription("Another Fine Description.");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setBasePath("/apiTwo");
beanConfig.setResourcePackage("com.test.rest.resource.internal");
beanConfig.setPrettyPrint(true);
// Set configId,contextId & scannerId
beanConfig.setConfigId("admin");
beanConfig.setContextId("admin");
beanConfig.setScannerId("admin");
beanConfig.setScan(true);
beanConfig.setScan(true);
}
您还应该指定configId,contextId&您的servlet配置中的scannerId作为init参数,如下所示.
<servlet>
<servlet-name>jersey-rest-public</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.rest.resource.PublicApplication</param-value>
</init-param>
<init-param>
<param-name>swagger.scanner.id</param-name>
<param-value>public</param-value>
</init-param>
<init-param>
<param-name>swagger.context.id</param-name>
<param-value>public</param-value>
</init-param>
<init-param>
<param-name>swagger.config.id</param-name>
<param-value>public</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>jersey-rest-admin</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.rest.resource.AdminApplication</param-value>
</init-param>
<init-param>
<param-name>swagger.context.id</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>swagger.scanner.id</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>swagger.config.id</param-name>
<param-value>admin</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
标签:java,web-services,jax-rs,swagger,swagger-2-0 来源: https://codeday.me/bug/20190519/1134256.html