Spring使用ObjectMapper和RepositoryRestMvcConfiguration引发循环引用错误

时间:2019-01-17 22:25:36

标签: spring jackson spring-data-rest objectmapper circular-reference

我有一个Spring Boot应用程序(2.0.3),它引发涉及objectMapper的循环引用错误(faster.xml Jackson实现,2.9.6)。使用Gradle(4.10.2)可以很好地构建它,但是在部署时会引发以下错误:

...; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.leroyjenkins.service.common.config.CommonConfig':
Unsatisfied dependency expressed through field 'objectMappers'; nested exception is
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'objectMapper' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]:
Circular reference involving containing bean 'org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration' - 
consider declaring the factory method as static for independence from its containing instance. Factory method 'objectMapper' threw exception; nested exception is java.lang.NullPointerException",
    "\tat org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)",

CommonConfig指的是我拥有的配置文件:

@Configuration
@EnableHypermediaSupport(type = . 
EnableHypermediaSupport.HypermediaType.HAL)
@EnableScheduling
@Order(Ordered.LOWEST_PRECEDENCE)
@CompileStatic
@Slf4j
@Slf4jPlusMetrics("mlog")
class CommonConfig {

...

@Autowired List<ObjectMapper> objectMappers

@PostConstruct
void afterPropertiesSet() {
    log.info("initializing CommonConfig objectMappersSize={},contextPath={},threadPoolSize={},threadPoolQueueCapacity={},threadPoolDefaultTimeout={},appName={}",
        objectMappers.size(), contextPath, threadPoolSize, threadPoolQueueCapacity, threadPoolDefaultTimeout, appName)

    objectMappers.each { ObjectMapper objectMapper ->
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
        objectMapper.registerModule(new ParameterNamesModule())
        objectMapper.registerModule(new Jdk8Module())
        objectMapper.registerModule(new JavaTimeModule())
        objectMapper.registerModule(new JodaModule())
        objectMapper.registerModule(new GsnModelJacksonModule())
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
    }
}

...

}

我们刚刚更新为jdk11 ...不确定此处是否相关。

0 个答案:

没有答案