简体   繁体   中英

Springfox swagger throws StackOverflowError

I'm trying to add springfox-swagger to my Spring MVC project. I have the following configuration:

@Configuration
@EnableSwagger2
@ComponentScan(basePackageClasses = com.mycompany.MyCtrl.class)
public class SpringFoxConfig {

    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2);
    }
}

@Configuration
@EnableWebMvc
@EnableCaching
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan(basePackageClasses = { ApplicationConfig.class, AppConfig.class})
@Import(SpringFoxConfig.class)
public class ApplicationConfig extends WebMvcConfigurerAdapter implements CachingConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

But after deploy I can't access to any controllers and after 1-5 mins I get the following stacktrace:

 2015-12-24 18:26:46,297 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-5) Context initialization failed: com.google.common.util.concurrent.ExecutionError: java.lang.StackOverflowError

 at springfox.documentation.spi.schema.contexts.ModelContext.hasSeenBefore(ModelContext.java:156) [springfox-spi-2.3.0.jar:2.3.0]

 at springfox.documentation.spi.schema.contexts.ModelContext.parentHasSeenBefore(ModelContext.java:174) [springfox-spi-2.3.0.jar:2.3.0]
at springfox.documentation.spi.schema.contexts.ModelContext.hasSeenBefore(ModelContext.java:157) [springfox-spi-2.3.0.jar:2.3.0]

Without springfox, my controllers work well. What I'm doing wrong?

I have seen this behavior when you have a master-details DTO with bidirectional references:

class Master {
    List<Detail> details;
}

class Details {
    Master master;
}

A workaround for this issue is to add hidden = true attribute to @ApiParam annotation on parameters of such types:

ResponseEnitity<?> controller(@ApiParam(hidden = true) @RequestBody Master master) {
}

I believe it is a bug in Springfox but I didn't have a chance to report it yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM