1

当我将 Spring 应用程序上传到 Pivotal Web Services 时,出现两个错误:

ERR Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.http.converter.json.MappingJackson2HttpMessageConverter]: Factory 
method 'jacksonHttpMessageConverter' threw exception; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'objectMapper' defined in class path resource
[io/app/config/AppRestMvcConfiguration.class]: Bean instantiation via factory method 
failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' 
threw exception; nested exception is java.lang.NullPointerException

Failed to instantiate [org.springframework.http.converter.json.MappingJackson2HttpMessageCo
nverter]: Factory method 'halJacksonHttpMessageConverter' threw exception; nested 
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'halObjectMapper' defined in class path resource [io/papped/config/PappedRestMvcC
onfiguration.class]: Bean instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'halObjectMapper' threw 
exception; nested exception is java.lang.NullPointerException

所以基本上,它无法在我的配置类中初始化 objectMapper 和 halObjectMapper(这些是在SpringBootRepositoryRestMvcConfiguration类中预定义的)。当我在我的计算机上本地运行应用程序时,不会发生这些错误。

我尝试删除我的配置类,但它仍然给出错误。我尝试使用以下代码手动实例化 objectMapper/halObjectMapper:

private final static ObjectMapper mapper = new ObjectMapper();

@Bean
@Primary
public com.fasterxml.jackson.databind.ObjectMapper objectMapper() {
    return mapper;
}

@Bean
@Primary
public ObjectMapper halObjectMapper() {
    return mapper;
}

这使得它可以在关键的 Web 服务上运行,但每当我尝试访问 MongoRepository 时都会出现堆栈溢出错误(我的模型中没有循环引用)。

如何解决?

4

1 回答 1

0

问题是我为云设置 mongo 存储库服务的方式。使用以下代码使其工作:

@Configuration
@Profile("!local")
public class DatabaseConfiguration extends AbstractCloudConfig {
    @Bean
    public MongoDbFactory documentMongoDbFactory() {
        return connectionFactory().mongoDbFactory();
    }
}
于 2016-01-28T17:36:20.113 回答