SpringFox依赖打破了我的Spring Context [英] SpringFox dependency breaking my Spring Context

查看:298
本文介绍了SpringFox依赖打破了我的Spring Context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近,我才开始大家免费获取我的招摇文档,因为我使用Camel来公开我的REST API。我不得不放弃Camel,因为它与杰克逊玩得很好,所以我试图建立SpringFox。不幸的是,简单地添加SpringFox作为依赖会以某种方式破坏我的Spring上下文,并抛出一个模糊且不正确的错误声称我没有一个显式的ObjectMapper bean,我这样做 - 无法使Camel使用所述bean是我必须删除的原因它。

Until recently I've been getting my swagger documentation mostly-free, because I was using Camel to expose my REST APIs. I've had to ditch Camel because of some problems it has playing nice with Jackson, so I'm trying to set up SpringFox. Unfortunately, simply adding SpringFox as a dependency wrecks my Spring context somehow, and throws a vague and incorrect error claiming I don't have an explicit ObjectMapper bean, which I do - the inability to make Camel use said bean is why I had to remove it.

要明确,不做任何代码更改,只需添加以下依赖项:

To be clear, without making any code changes, just adding the following dependency:

compile group: "io.springfox", name: "springfox-swagger2" version: "2.0.2"

导致问题。我正在使用Jackson 2.5.3和Spring Boot 1.2.3.RELEASE

Causes the problem. I'm using Jackson 2.5.3 and Spring Boot 1.2.3.RELEASE

FWIW这里是堆栈跟踪的有趣部分:

FWIW here's the interesting part of the stack trace:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration$HypermediaConfiguration$HalObjectMapperConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fasterxml.jackson.databind.ObjectMapper org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration$HypermediaConfiguration$HalObjectMapperConfiguration.primaryObjectMapper; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.fasterxml.jackson.databind.ObjectMapper] is defined: expected single matching bean but found 2: objectMapper,_halObjectMapper
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:101)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
    ... 45 more


推荐答案

我有同样的问题。我终于能够将其缩小到根本原因,在我的案例中证明是@Dilip在上面指出的。

I had the same exact issue. I was finally able to narrow it down to the root cause, which in my case turned out to be what @Dilip pointed out above.

不知何故,HypermediaSupport正在启用我包含 springfox 依赖项时的项目,即使我的项目中没有显式的 @EnableHypermediaSupport 注释。我认为Spring是自动启用它的,因为我的主java配置类中有 @EnableAutoConfiguration

Somehow, HypermediaSupport was getting enabled in my project when I include springfox dependencies, even though I didn't have the @EnableHypermediaSupport annotation explicitly in my project. I think Spring was enabling it automagically because I had @EnableAutoConfiguration in my main java configuration class.

我能够通过在我的java配置类中通过 @EnableAutoConfiguration(exclude = {HypermediaAutoConfiguration.class})注释排除'HypermediaAutoConfiguration'来克服这个问题。

I was able to overcome this issue by excluding 'HypermediaAutoConfiguration' via the @EnableAutoConfiguration(exclude = {HypermediaAutoConfiguration.class}) annotation in my java config class.

在我执行此步骤后,异常消失了。希望这可以帮助。

After I did this step, the exception went away. Hope this helps.

这篇关于SpringFox依赖打破了我的Spring Context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆