SpringFox dependency on my Spring context

Until recently, I received my documentation for the most part for free, because I used Camel to expose my REST APIs. I had to deflate Camel due to some problems that it plays well with Jackson, so I'm trying to configure SpringFox. Unfortunately, just adding SpringFox as dependencies somehow destroys my Spring context and generates an undefined and incorrect error, claiming that I don't have an explicit ObjectMapper bean that I am doing - the inability to get Camel to use the bean why I should have deleted it.

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

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

Causes a problem. I am using Jackson 2.5.3 and Spring Boot 1.2.3.RELEASE

FWIW here is 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
+4
source share
2 answers

I had the exact same problem. Finally, I was 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 included in my project when I included dependencies springfox, although I did not have the annotations @EnableHypermediaSupportexplicitly in my project. I think Spring automatically included this because I had @EnableAutoConfigurationin my main java configuration class.

I managed to overcome this problem by excluding "HypermediaAutoConfiguration" using annotation @EnableAutoConfiguration(exclude = {HypermediaAutoConfiguration.class})in my java configuration class.

After this step, the exception has disappeared. Hope this helps.

+9

, :

@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 - .

+1

Source: https://habr.com/ru/post/1584117/


All Articles