6

I'm working on a spring boot application using swagger to generate docs for my API ,I'm using Spring data rest to generate the Api but when I run the app I get the swagger message : No operations defined in spec!

This my Api code :

@Api(tags = "projets")
@RepositoryRestResource(collectionResourceRel = "projets", path = "projets")
public interface IProjectRepository extends JpaRepository<Project, Long> {

}

And this my configuration file :

@Configuration
@EnableSwagger2
public class QfactoryConfiguration {

    @Bean
     public Docket getDocketInstance() {
         return new Docket(DocumentationType.SWAGGER_2) 
                 .apiInfo(new ApiInfoBuilder()
                            .title("Spring Boot project")
                            .description("Spring Boot bootstrap project")
                            .version("0.1")
                            .license("Unlicense")
                            .build())
                  .select()  
                  .apis(RequestHandlerSelectors.basePackage("com.errabi.qfactory.repositories"))             
                  .paths(PathSelectors.any())                          
                  .build();  
     }




}

And this the dependencies that I'm using :

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

I'm asking is the Swagger is able to generate docs for Api generated by Spring data rest or I should use a @RestController with annotations @Api,@ApiOperation

I'm using spring boot version : 2.1.3.RELEASE

e2rabi
  • 4,728
  • 9
  • 42
  • 69

5 Answers5

3

This did the trick for me, upgrading to the latest snapshot of springfox

<dependencies>
...
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-data-rest</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
...
</dependencies>

<repositories>
    <repository>
        <id>JFrog</id>
        <name>JFrog Snapshot Repository</name>
        <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
    </repository>
</repositories>

Then use @EnableSwagger2WebMvc instead of @EnableSwagger2:

@SpringBootApplication
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class MyApplication {

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

}

All thanks to Yann39.

tjheslin1
  • 1,378
  • 6
  • 19
  • 36
  • I'm also facing the same issue. Swagger UI doesn't show the default endpoint available by RepositoryRestResource annotation. I've spent a whole day on this. Fixed some issues by pointing to 3.0.0 snapshot. But still, I don't see the endpoints are showing in swagger UI. I can able to see only the @RestController endpoints. Let me know if this is working for you. – ARods Aug 09 '19 at 04:54
  • I am on Spring 2.2.2.RELEASE - it seems that `SpringDataRestConfiguration` got removed. Or renamed? Any ideas? See https://stackoverflow.com/questions/59868163/why-is-springfox-not-exposing-repositoryrestresource – Stefan Falk Jan 23 '20 at 18:42
  • Even I am also facing the same issue. I am using Spring Boot 2.2.2.RELEASE, added SpringFox 3.0.0-SNAPSHOT dependency. When I try to access swagger-ui.html page I am getting Whitelabel error page. – Satya P Jun 27 '20 at 15:48
  • Are you having any security enabled? did you got anything on console pointing to any details?! you can check this issue and it's related answers if you are having the same kind of exception. https://stackoverflow.com/a/57342184/9019797 – Mohammed Salah Jun 28 '20 at 08:30
  • EnableSwagger2WebMvc is deprecated in the release 3.0.0, not sure about the earlier ones. – pidabrow Jun 08 '21 at 14:06
1

Based on the discussions on this springfox Github issue looks like you need to amend your config class to include an additional annotation, i.e.:

@Configuration
@EnableSwagger2
@Import({springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class})

I can't test it as I only use swagger for Rest controllers at present but hope this helps.

Paul
  • 86
  • 4
  • 1
    I added the SpringDataRestConfiguration and the application fails at start up searching about the error I found that Swagger is not compatible with spring boot 2.X this the erro Correct the classpath of your application so that it contains a single, compatible version of org.springframework.data.repository.support.Repositories – e2rabi Feb 23 '19 at 22:18
  • What does the rest of your POM look like? – Paul Feb 23 '19 at 22:27
  • @EnableSwagger2WebMvc is deprecated , that's correct what u ve done – Tiago Medici Oct 27 '20 at 20:43
1

Try RequestHandlerSelectors.any() instead of RequestHandlerSelectors.basePackage("").

double-beep
  • 5,031
  • 17
  • 33
  • 41
Surya
  • 11
  • 1
0

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <swagger>3.0.0-SNAPSHOT</swagger>
</properties>

<dependencies>
<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger}</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger}</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>${swagger}</version>
    </dependency>

</dependencies>

application.yaml

spring:
    data:
        rest: 
            base-path: /v1

java config

@Configuration
@EnableSwagger2WebMvc
@Import(SpringDataRestConfiguration.class)
public class SwaggerConfig 
Tiago Medici
  • 1,944
  • 22
  • 22
0

Verify your package & add

@Configuration
@EnableSwagger2

to the spring boot configuration file

   @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("your.package.name")).paths(PathSelectors.any()).build()
                .apiInfo(apiEndPointInfo());
    }

only those dependencies are required

<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>

your final config file should looks like this

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@Configuration
@EnableSwagger2
public class TestappApplication implements WebMvcConfigurer {

    private static final String PREFIX = "/WEB-INF/views/";
    private static final String SUFFIX = ".jsp";

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

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp(PREFIX, SUFFIX);

    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("your.package.scan.to.swagger.annotations")).paths(PathSelectors.any()).build()
                .apiInfo(apiEndPointInfo());
    }

    public ApiInfo apiEndPointInfo() {
        return new ApiInfoBuilder().title("Spring Boot Rest API").description("user Management API")
                .contact(new Contact("zouhair kasmi", "website.com/", "myemail@gmail.com"))
                .license("Apache 2.0").licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .version("0.0.1-SNAPSHOT").build();

    }

}
Zouhair Kasmi
  • 614
  • 2
  • 6
  • 13