【问题标题】:Is there a way I can stop springfox swagger from scanning the model classes?有没有办法阻止springfox swagger扫描模型类?
【发布时间】:2017-10-09 16:52:19
【问题描述】:

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

【问题讨论】:

  • 也许你看起来像this ??假设当你的 API 进入生产阶段时,你的主模型不会改变,因此在每次重启时都会扫描它......

标签: spring spring-boot swagger springfox


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用 swagger 3.0 @Hidden 注释可用,该注释位于 io.swagger.v3.oas.annotations 包中

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

    【讨论】:

      【解决方案3】:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-30
        • 2021-02-28
        • 1970-01-01
        • 2019-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多