10

I'm currently using Springfox Swagger to document my spring boot application with a Java config. My API starts in about 75 seconds, (it was originally 20 secs without Springfox) with the whole scanning process. I currently just need the controller information without any model info. Is there a way I can exclude model scanning from the startup process in order to make my API start faster? And are there any other ways to make it faster? I'm using swagger 1.2

Oliver
  • 11,857
  • 2
  • 36
  • 42
kugar
  • 129
  • 1
  • 1
  • 6
  • maybe you are looking something like [this](https://stackoverflow.com/questions/27442300/disabling-swagger-with-spring-mvc) ?? Suppose that when your API goes to Production Phase, your main model is not going to change , thus scanning it in every single restart.... – AntJavaDev Oct 09 '17 at 16:59

3 Answers3

16

There is a way to prevent Sprinfox framework from generating a Swagger model or parameter information of specified ignored types. You have to use the method ignoredParameterTypes in SwaggerSpringMvcPlugin or Docket class to let it know the types to be ignored.

Swagger 1 Example

Here is an example of Swagger 1 Java configuration with ignored types. It definitely had an impact on my application startup time.

@Configuration
@EnableSwagger
public class SwaggerConfiguration {

    @Autowired
    private SpringSwaggerConfig springSwaggerConfig;

    @Bean
    public SwaggerSpringMvcPlugin api() {
        Class[] clazz = {MyClassA.class, MyClassB.class};

        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
                .apiInfo(apiInfo())
                ...
                .ignoredParameterTypes(clazz);
    }

     private ApiInfo apiInfo() {
         ...
     }
}

Swagger 2 Example

Here is an example of Swagger 2 Java configuration with ignored types,

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket api() {
        Class[] clazz = {MyClassA.class, MyClassB.class};

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("my-group")
                .select()
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .ignoredParameterTypes(clazz);
    }

     private ApiInfo apiInfo() {
         ...
     }
}
Indra Basak
  • 7,124
  • 1
  • 26
  • 45
1

With swagger 3.0 @Hidden annotation is available which is present at package io.swagger.v3.oas.annotations

It can be used on top of the class or method to exclude the resource in swagger documentation.

0

Springfox Swagger2 acquire UI data through GET /v2/api-docs, which will mapping to springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation().So you can just create a bean to take place of 'ServiceModelToSwagger2Mapper' with your sanning logic:

@Primary
@Component
class CustomServiceModelToSwagger2Mapper : ServiceModelToSwagger2MapperImpl() {
      override fun mapDocumentation(from: Documentation?): Swagger? {

               // scanning logics...
      }
}

refer to my another related answer : https://stackoverflow.com/a/64057512/14332259

nanlan
  • 1