Cannot get my endpoints list from controller in swagger UI

I am new to Spring boot with Swagger UI. I’m just trying to configure my Rest controller endpoints to show on swagger UI screen but it shows No operations for specs defined. Pretty sure, its a configuration issue.

I have tried @EnableAutoConfiguration, still it can’t find the controller

SwaggerDemoApplication.java

package com.example.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SwaggerDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SwaggerDemoApplication.class, args);
 }

}

SwaggerConfig.java

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;

@EnableSwagger2
@Configuration

public class SwaggerConfig {

 @Bean
 public Docket productApi() {
     return new Docket(DocumentationType.SWAGGER_2)  
              .select()                                  
              .apis(RequestHandlerSelectors.basePackage("com.example.controller"))              
              .paths(regex("/test.*"))                       
              .build();                                           

 } 
}



TestController.java

package com.example.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

@RestController
@RequestMapping(value = "/test")
@Api(value="onlinestore", description="Operations pertaining to products in Online Store")
public class TestController {

    @RequestMapping(value = "/test-swagger", method= RequestMethod.GET)
    public String home() {
        return "Spring is here!";
    }

}

Expected: Rest endpoint Actual: No operations defined in spec

Answer

The problem is not your Swagger Configuration but your Controller is not scanned as Spring resource class.

Because your application startup class (Main class) has no @ComponentScan annotation.

So, your class should be like this:

@EnableSwagger2
@SpringBootApplication
@ComponentScan("com.example")
public class SwaggerDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SwaggerDemoApplication.class, args);
    }
}

Now you can access your API documentation from here http://localhost:8098/swagger-ui.html#

Leave a Reply

Your email address will not be published. Required fields are marked *