Shivanshu Goyal
Shivanshu Goyal

Reputation: 530

Swagger Configuration for controllers from different packages in spring-boot application

I have rest controllers in a spring-boot application.

Project structure:

Controllers

Services

Models

localhost:8080/app/swagger-ui.html shows only one package controllers.

ControllerClass1

     Method1 GET 

     Method2 POST

ControllerClass2

     Method1 PUT

     Method2 POST

I want to put all controllers from all packages on swagger UI.

Upvotes: 1

Views: 3393

Answers (2)

Shivanshu Goyal
Shivanshu Goyal

Reputation: 530

It is fixed by adding the base package

@Bean
public Docket productApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.shivanshu.project"))
            .build();

}

Upvotes: 1

Beppe C
Beppe C

Reputation: 13883

you can configure packages and classes you want instructing Swagger which paths to include

.paths(Predicates.or(
                    PathSelectors.ant("/api/v1/pck1/**"),
                    PathSelectors.ant("/api/v1/pck2/**"),
                    PathSelectors.ant("/api/anotherpath/**"))
            )

The paths are defined in the Controller (it doesnt matter which package)

package com.example.controller;
@RestController
@RequestMapping("/api/v1/pck1")
public class MyController {

Upvotes: 0

Related Questions