Multiple resource packages with Swagger 2

10,878

Solution 1

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.

Solution 2

If all the API are in same project but in different packages then You can give the name of the base package of both like this.

com.company1.resources1 org.company1.resources2 Then give only name of one like this- then it will List all Controllers.

  @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors
                    .basePackage("com.company1"))
                .paths(PathSelectors.regex("/.*"))
                .build().apiInfo(apiEndPointsInfo());
        }
Share:
10,878

Related videos on Youtube

garci560
Author by

garci560

Updated on October 23, 2022

Comments

  • garci560
    garci560 over 1 year

    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.

  • dbudzins
    dbudzins almost 3 years
    I think you meant com.company1 for both resources1 and resources2, right?