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

2023-09-07 13:53:52 作者:其实很在乎°

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

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

推荐答案

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

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 Java 配置示例.它肯定对我的应用程序启动时间产生了影响.

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

这是一个带有忽略类型的 Swagger 2 Java 配置示例,

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