Search code examples
spring-mvcspring-bootspring-dataspring-restcontrollerlightadmin

LightAdmin - Customise parsing DateTime with app timezone


I am using LightAdmin 1.1.0.Snapshot with Spring Boot. I am using Joda DateTime to represent time with zone.

I can see LightAdmin captures date-time in UTC and Default Deserialization context used for parsing data is by UTC in LightAdmin. From debugging, I see LightAdmin uses its own ObjectMapper and MessageConverters using LightAdminRestMvcConfiguration, so it is not using the Spring Boot global overriders for customising the Jackson2ObjectMapperBuilder like the one below.

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.timeZone(coreProperties.appTimeZone());
    return builder;
}

Any help on how to 1) override settings for Jackson in LightAdmin to parse with default app timezone or 2) Handle Json serialization / converter outside LightAdmin to solve this problem differently. Any help would be awesome.

Thanks, Alex


Solution

  • One way I solved the problem is to reconfigure the LightAdmin beans after the context is loaded using the below.

    @Component
    public class AppContextListener implements ApplicationListener<ContextRefreshedEvent>{
    
        @Inject
        CoreProperties coreProperties;
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
    
            GenericWebApplicationContext appContext = getRootApplicationContext(event);
            WebApplicationContext lightAdminWebContext = getWebApplicationContext(appContext.getServletContext(), LightAdminWebApplicationInitializer.SERVLET_CONTEXT_ATTRIBUTE_NAME);
            lightAdminWebContext.getBeansOfType(ObjectMapper.class).values().stream()
                        .forEach(objectMapper -> objectMapper.setTimeZone(coreProperties.appTimeZone()));
        }
    
        private GenericWebApplicationContext getRootApplicationContext(ContextRefreshedEvent event) {
            return (GenericWebApplicationContext) (event.getApplicationContext().getParent() != null ? event.getApplicationContext().getParent() : event.getApplicationContext());
        }
    }