1

I am having trouble configuring the default ObjectMapper in JHipster to allow JsonViews and also setting to false the FORCE_LAZY_LOADING property of the Hibernate4Module module.

I have tried three things without success:

1) Create a @Bean using the @Primary annotation to replace the default bean:

@Bean
@Primary
public ObjectMapper viewsObjectMapper(){ 
     ObjectMapper mapper = new ObjectMapper();
     Hibernate4Module hibernateModule = new Hibernate4Module(); 
     hibernateModule.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, false);       mapper.registerModule(hibernateModule);
     mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
     return mapper;
 }

2) I modified the @Bean Hibernate4Module in the DatabaseConfiguration class as follows:

@Bean
public Hibernate4Module hibernate4Module() {
    Hibernate4Module hibernateModule = new Hibernate4Module();
    hibernateModule.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, true);
    return hibernateModule;
}

3) And this solution.

Any help will be appreciated.

Community
  • 1
  • 1
Gorayni
  • 606
  • 2
  • 8
  • 26

1 Answers1

3

You can do it as stated in Spring Boot's documentation. In Spring Boot, it is not necessary to declare the ObjectMapper in a @Configuration class that extends the WebMvcConfigurationSupport. JHipster creates a @Configuration class named WebConfigurer where you can put this code:

@Bean
public ObjectMapper viewsObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);    
    Hibernate4Module hibernateModule = new Hibernate4Module();
    hibernateModule.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, false);          
    objectMapper.registerModule(hibernateModule);       
    return objectMapper;
}

@Bean
public MappingJackson2HttpMessageConverter jackson2Converter() {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(viewsObjectMapper());          
    return converter;
} 
Jean Montague
  • 116
  • 1
  • 2