Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple resource packages with Swagger 2

Is there any way of specifying multiple packages when configuring Swagger 2?

    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setVersion("1.0.0");
    beanConfig.setResourcePackage("com.company1.resources ; org.company2.resources");
    beanConfig.setScan(true);

BeanConfig.setResourcePackage only takes a String. If I separate them with a space, only the first one is considered. If I try with a semicolon, swagger doesn't find any.

I have a Spring Boot application with Jersey 2.

like image 407
garci560 Avatar asked Jul 21 '16 10:07

garci560


People also ask

How do I add multiple API to Swagger?

For example in spring (springfox-swagger) you need just to put the same tag on multiple API classes and it will merge them in one group in the swagger UI. @Api(value = "First API", tags = {"first-api"}) public class FirstApi { ... } @Api(tags = {"first-api"}) public class SecondApi { ... }

What is Swagger 2 in Spring boot?

React Full Stack Web Development With Spring Boot Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser.

Can we use Swagger in production?

7-p3), Swagger is disabled automatically in production mode. This means that you can still use it on development machines where it is most important, but not on the live server. It's not possible to easily use Swagger on test or staging systems either while they run in production mode (which they should).

What does Swagger most popular for?

Swagger is the name associated with some of the most well-known, and widely used tools for implementing the OpenAPI specification. The Swagger toolset includes a mix of open source, free, and commercial tools, which can be used at different stages of the API lifecycle.


1 Answers

I have not seen this documented anywhere, but based on the code in the current Swagger core (1.5.10) it looks like you separate multiple packages by a comma (','). Here is the relevant snippet from BeanConfig.java:

    if (resourcePackage != null && !"".equals(resourcePackage)) {
        String[] parts = resourcePackage.split(",");
        for (String pkg : parts) {
            if (!"".equals(pkg)) {
                acceptablePackages.add(pkg);
                config.addUrls(ClasspathHelper.forPackage(pkg));
            }
        }
    } else {
        allowAllPackages = true;
    }

Further down it appears the allowAllPackages flag causes Swagger to generate documentation on all classes in the application's classpath that have JAX-RS annotations. Depending on what dependencies you have, simply not setting a resource package at all might be an option. Probably safer to use the comma-separated list, though.

Edit:

I did find mention of this in the documentation:

setResourcePackage(String)

resourcePackage

Sets which package(s) Swagger should scan to pick up resources. If there's more than one package, it can be a list of comma-separated packages.

like image 129
vonmoltke Avatar answered Oct 09 '22 17:10

vonmoltke