SpringFox dependency breaking my Spring Context

◇◆丶佛笑我妖孽 提交于 2019-12-22 09:14:29

问题


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

回答1:


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.




回答2:


You need to add your converts like example below:

@Configuration
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

        @Override
        protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(converter());
                addDefaultHttpMessageConverters(converters);
        }

        @Bean
        MappingJackson2HttpMessageConverter converter() {
                return new MappingJackson2HttpMessageConverter(new JacksonObjectMapper().getObjectMapper());
        }

}

*JacksonObjectMapper is my mapper class.



来源:https://stackoverflow.com/questions/31307883/springfox-dependency-breaking-my-spring-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!