Pivotal Web Services Spring Error

576 Views Asked by At

When I upload my Spring application to Pivotal Web Services, I get the two errors:

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

and

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

So basically, it can't initialise the objectMapper and halObjectMapper in my configuration class (these are predefined in the SpringBootRepositoryRestMvcConfiguration class). These errors do not occur when I run the app locally on my computer.

I have tried removing my config class but it still gives the error. I have tried manually instantiating the objectMapper/halObjectMapper by using the following code:

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;
}

This makes it run on pivotal web services, but gives a stack overflow error any time I try to access a MongoRepository (there are no circular references in my models).

How can this be solved?

1

There are 1 best solutions below

0
On BEST ANSWER

The problem was the way I was setting up the mongo repository service for the cloud. Using the following code made it work:

@Configuration
@Profile("!local")
public class DatabaseConfiguration extends AbstractCloudConfig {
    @Bean
    public MongoDbFactory documentMongoDbFactory() {
        return connectionFactory().mongoDbFactory();
    }
}