Using swagger with multiple API applications, swagger.json documents get mixed up
See original GitHub issueWe are running two separate Jersey APIs in the same server context, il.e. two separate classes extending com.sun.jersey.api.core.PackagesResourceConfig are configured in Tomcat 7’s web.xml to be two accessible under two separate base paths. Simplified it’s:
<servlet>
<servlet-name>Api1</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>api1.server.config.Api1ServerConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Api1</servlet-name>
<url-pattern>/api1/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Api2</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>api2.server.config.Api2ServerConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Api2</servlet-name>
<url-pattern>/api2/*</url-pattern>
</servlet-mapping>
The two APIs do not share any model classes or other code and are completely separate in every aspect incl security, filter chains etc. There are two separate application.wadl files (and their corresponding XSD schema) generated by Jersey, each describing only the resources from their respective API.
We are using swagger in the first API, configuring it using BeanConfig:
BeanConfig bc = new BeanConfig();
bc.setVersion("1.0");
bc.setTitle(" API1 Documentation");
bc.setSchemes(new String[]{"https"});
bc.setBasePath("/appRoot/api1");
bc.setResourcePackage("api1.resource");
bc.setScan(true);
Now we try to add Swagger to the second API as well, by declaring this in our Api2ServerConfig:
BeanConfig bc = new BeanConfig();
bc.setVersion("1.0");
bc.setTitle(" API2 Documentation");
bc.setSchemes(new String[]{"https"});
bc.setBasePath("/appRoot/api2");
bc.setResourcePackage("api2.resource");
bc.setScan(true);
Unfortunately while this results in two separate swagger.json documents under /appRot/api1/swagger.json and /appRoot/api2/swagger.json, the two APIs are not treated separately as we had hoped.
The contents of whichever swagger document we access first after starting the server are fine. If we access /appRoot/api1/swagger.json, it contains all the paths and types of API1 and nothing from API2’s realm. If however we next access /appRoot/api2/swagger.json, it lists both its own resources AND every tag, path and type definition from API1. Vice versa, if /appRoot/api2/swagger.json is accessed first and then appRoot/api1/swagger.json, API2’s document is correct, but itself is now leaking into API1’s swagger.json. The title and basePath are correct in both documents.
Is there anything else that needs to be done to keep multiple APIs from mixing, or is it just not possible at all?
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (2 by maintainers)
The Problem that is described in the first comment from andi-livn is still not solved. Should I open a new Ticket?
Looking at
BeanConfig
, it looks like you can have multiple packages, separated by,
. Can you please try that?