No operations defined in spec! with spring boot

Viewed 2235

I'm getting No operations defined in spec! while loading swagger-ui on sping boot

Below are code details:

pom.xml
 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.4.8</version>
        </dependency>

On my main file,
   
@OpenAPIDefinition(
        info = @Info(
                title = "RESTAPI",
                //version = "${app.version}",
                description = "svsjsjj ssksj",
                contact = @Contact(
                            name = "bajaj", 
                            url = "https://jhakja.com"
                )
        )
)

@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages = { "io.swagger", "io.swagger.api" , "io.swagger.configuration"})
public class Swagger2SpringBoot extends SpringBootServletInitializer implements CommandLineRunner  {

// I have sqlitcode + Date and time code,

    public static void main(String[] args) throws Exception {
     new SpringApplication(Swagger2SpringBoot.class).run(args);
    }

    @Bean
    public Docket customImplementation(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                    .apis(RequestHandlerSelectors.basePackage("io.swagger.api"))
                    .build();
    }
}

I just tried adding as below on my application.properties :

springdoc.paths-to-exclude=/swagger-resources/**      //wanted to exclude swagger-resource
springdoc.packagesToScan=io.swagger.api
springdoc.pathsToMatch=restapi/v2,restapi/v2/*

I have many controllers with below forms since project was generated from swagger.io -> exported as spring project -> imported the same on IDE

  1. interafce :

    @Validated
    @Api(value = "alert", description = "the alert API")
    @RequestMapping(value = "/v2")
    public interface AlertApi {
    
     @ApiOperation(value = "Finds all alerts", nickname = "findAllAlerts", notes = "Provides list of all alerts", responseContainer = "List", authorizations = {
         @Authorization(value = "api_key"),
         @Authorization(value = "settings_auth", scopes = {
             @AuthorizationScope(scope = "write:settings", description = "modify settings in your system"),
             @AuthorizationScope(scope = "read:settings", description = "read your settings")
             })
     }, tags={ "alert", })
     @ApiResponses(value = { 
         @ApiResponse(code = 200, message = "successful operation", responseContainer = "List"),
         @ApiResponse(code = 400, message = "Invalid status value") })
     @RequestMapping(value = "/alert/history",
         produces = {"application/json" },
         method = RequestMethod.GET)
     ResponseEntity<Object>  findAllAlerts();
    
     }
    
  2. class file :

      @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2021-10-12T11:46:11.648Z")
    
      @Controller
      public class AlertApiController implements AlertApi {
    
     public ResponseEntity<Object> findAllAlerts() {
      // processing 
    
     }
     }
    

I also tried creating a single class file instead of interface and then class but that didnt work.

@Tag(name = "PingController", description = "This is responsible for give the status of application")
@RestController
@RequestMapping(restapi/v2)
public class PingController {


    @Operation(summary = "End-point to test ping")
    @GetMapping("/v2/ping")
    public ResponseEntity<String> getMessages() {
    //other code    
    }
}

I have jwt-related code as well but moved to different packages.

Not able to load the controllers. Need help on this.

swagger ui displayed

Thanks in advance!

0 Answers
Related