I am setting the timezone of the app as below :
@SpringBootApplication
public class SpringApp extends SpringBootServletInitializer {
/**
* Application startup.
*
* @param args
*/
public static void main(final String[] args) {
SpringApplication.run(SpringApp.class, args);
}
/**
* Setting time zone.
*/
@PostConstruct
public void init() {
// Setting Spring Boot SetTimeZone
log.info("time zone set to {}", timeZone);
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
}
}
But when I am using the restTemplate I had to set the timeZone explicitly like below :
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
ObjectMapper mapper = new ObjectMapper();
// Register the Java Time Module
mapper.registerModule(new JavaTimeModule());
// Set the default time zone to BST (Europe/London)
mapper.setTimeZone(TimeZone.getTimeZone("Europe/London"));
// Configure the RestTemplate to use the customized ObjectMapper
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper);
restTemplate.getMessageConverters().add(0, converter);
return restTemplate;
}
}
I was assuming that restTemplate will inherit the app timeZone settings, I think it should? Can you please check and make it possible because it really confusing.
Comment From: snicoll
I am not sure what you're asking us, really. You are configuring the RestTemplate
manually so I don't know what you expect us to do. ObjectMapper
is not part of Spring, and the default timezone is not set in the Spring context but on the JVM itself.