直到最近,我一直在免费提供我的招摇文档,因为我使用Camel来公开我的REST API。我不得不抛弃骆驼,因为它与杰克逊玩得很好,所以我试图建立SpringFox。不幸的是,简单地添加SpringFox作为依赖项会以某种方式破坏我的Spring上下文,并抛出一个模糊且不正确的错误声称我没有显式的ObjectMapper bean,我这样做 - 无法使Camel使用所述bean是我必须删除的原因它。
要明确,不进行任何代码更改,只需添加以下依赖项:
compile group: "io.springfox", name: "springfox-swagger2" version: "2.0.2"
导致问题。我正在使用Jackson 2.5.3和Spring Boot 1.2.3.RELEASE
FWIW这里是堆栈跟踪的有趣部分:
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
答案 0 :(得分:9)
我有同样的问题。我终于能够把它缩小到根本原因,在我的案例中证明是@Dilip在上面指出的。
不知何故,当我包含springfox
依赖项时,我的项目中启用了HypermediaSupport,即使我的项目中没有显式@EnableHypermediaSupport
注释。我认为Spring是自动启用它的,因为我的主java配置类中有@EnableAutoConfiguration
。
我能够通过我的java配置类中的@EnableAutoConfiguration(exclude = {HypermediaAutoConfiguration.class})
注释排除'HypermediaAutoConfiguration'来克服这个问题。
在我完成这一步后,异常消失了。希望这可以帮助。
答案 1 :(得分:1)
您需要添加您的转换器,如下例所示:
@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是我的映射器类。