Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringFox dependency breaking my Spring Context

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"

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

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
like image 982
Sophistifunk Avatar asked Dec 15 '22 13:12

Sophistifunk


1 Answers

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.

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.

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.

like image 88
stirfries Avatar answered Dec 17 '22 01:12

stirfries