Search code examples
spring-bootswaggeropenapiswagger-codegenspringfox

Getting swagger-ui to display my codegen-generated API


I'm working on a springboot project. We're doing API first so we're generating code from an api.yaml. We're using openapi 3.0. The interfaces are being generated fine but when we browse to our swagger-ui URL, it says No operations defined in spec!

Here are the details:

@Configuration
@RequiredArgsConstructor
@EnableSwagger2
public class SwaggerConfig {

  private final BuildProperties buildProperties;

  @Bean
  public Docket docketConfig() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors
            .basePackage("com.xyz.infrastructure.rest.spring.resource"))
        .build().apiInfo(apiInfo());
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title(buildProperties.getName())
        .version(buildProperties.getVersion())
        .build();
  }
}

Our structure is:

com.xyz.infrastructure.rest.spring | |- config |- SwaggerConfig |- spec //autogenerated |- dto //autogenerated |- resource // implementations of interfaces found in spec

What are we missing?

We're using:

<dependency>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen</artifactId>
  <version>3.0.21</version>
</dependency>
<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>

Thank you!


Solution

  • We finally got it to work. Here's what did it for us:

    Springfox version 3:

    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-boot-starter</artifactId>
       <version>3.0.0</version>
    </dependency>
    

    Using the io.swagger.codegen.v3 plugin.

    This swagger config:

    @Configuration
    @EnableOpenApi
    public class SwaggerConfig {
    
      @Bean
      public Docket docketConfig() {
        return new Docket(DocumentationType.OAS_30)
            .select()
            .apis(RequestHandlerSelectors
                .basePackage("com.xyz.infrastructure.rest.spring.resource"))
            .build();
      }
    
    }
    

    And make sure your classes in resource have the @RestController annotation.