2015-12-24 53 views
2

我想添加springfox-swagger到我的Spring MVC项目。我有以下配置:Springfox Swagger抛出StackOverflowError

@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/"); 
    } 
} 

但部署后,我无法访问任何控制器和后1-5分钟,我得到以下堆栈跟踪:

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] 

没有springfox,我的控制器正常工作。我做错了什么?

+0

查看您的日志可能会提供有关哪种模型导致问题的线索。 –

+0

您是否尝试升级到2.6.0以查看您的问题是否已修复? –

回答

0

我已经看到了这种行为,当你有一个主细节DTO双向引用:

class Master { 
    List<Detail> details; 
} 

class Details { 
    Master master; 
} 

针对此问题的解决方法是添加hidden = true属性@ApiParam注释这种类型的参数:

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

我相信这是Springfox中的一个bug,但我没有机会报告它。

相关问题