有没有办法可以阻止springfox swagger扫描模型类?

时间:2017-10-09 16:52:20

标签: spring spring-boot swagger springfox

我目前正在使用Springfox Swagger通过Java配置来记录我的Spring启动应用程序。在整个扫描过程中,我的API在大约75秒内启动(最初是20秒,没有Springfox)。我目前只需要控制器信息而无需任何型号信息。有没有办法可以从启动过程中排除模型扫描,以使我的API启动更快?还有其他方法可以让它更快吗?我使用了swagger 1.2

3 个答案:

答案 0 :(得分:5)

有一种方法可以防止Sprinfox框架生成Swagger模型或指定忽略类型的参数信息。您必须使用ignoredParameterTypesSwaggerSpringMvcPlugin类中的方法Docket来让它知道要忽略的类型。

Swagger 1示例

以下是带有忽略类型的Swagger 1 Java配置示例。它肯定会对我的应用程序启动时间产生影响。

@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示例

以下是带有忽略类型的Swagger 2 Java配置的示例

@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() {
         ...
     }
}

答案 1 :(得分:1)

使用swagger 3.0 @Hidden批注,该批注存在于io.swagger.v3.oas.annotations包中

可以在类或方法的顶部使用它,以排除swagger文档中的资源。

答案 2 :(得分:0)

Springfox Swagger2通过GET / v2 / api-docs获取UI数据,该数据将映射到springfox.documentation.swagger2.web.Sw​​agger2Controller.getDocumentation()。因此,您可以创建一个bean来代替“ ServiceModelToSwagger2Mapper”,您的清理逻辑:

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

               // scanning logics...
      }
}

请参阅我的另一个相关答案:https://stackoverflow.com/a/64057512/14332259