1

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?

Tometoyou
  • 7,792
  • 12
  • 62
  • 108
  • What have you tried ? Do you have any further information which can help solving your problem ? – Magix Jan 27 '16 at 13:01
  • @Magix I've updated my answer! – Tometoyou Jan 27 '16 at 13:22
  • I really can't see why pushing an app to PWS would result in such outcome (Especially the NPE part). Are you sure that the app locally works? Maybe you have some special beans in a `cloud` profile or something. What happens if you run your app locally with the `cloud` profile enabled? – Stephane Nicoll Jan 28 '16 at 09:13
  • @StéphaneNicoll I solved it by changing the way I set up the mongo repository. The Cloud Foundry website that I got the code from had written it wrongly. – Tometoyou Jan 28 '16 at 17:38
  • OK. Might worth reporting that. I had a look at your answer and if you bind the mongo service to your app there's nothing to do, Spring Boot will auto-create the connection factory for you. – Stephane Nicoll Jan 29 '16 at 06:50

1 Answers1

0

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();
    }
}
Tometoyou
  • 7,792
  • 12
  • 62
  • 108