I am trying to configure my ObjectMapper to have a specific TimeZone instead of using the Default GMT. I followed the instructions from different places (see references below) trying to configure via code and by XML. After debugging I notice that the code does set the ObjectMapper's TimeZone setting once, however during start up the Jackson2ObjectMapperBuilder class is called multiple times and overrides (null'ing) my TimeZone setting and returns to default GMT by the completion of the startup process, both in Tomcat and in my unit test.
Some code:
Config:
@Configuration
@EnableWebMvc
public class JacksonMapperConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
}
@Bean
public MappingJackson2HttpMessageConverter converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(jacksonObjectMapper());
return converter;
}
@Bean
public ObjectMapper jacksonObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setTimeZone(TimeZone.getTimeZone("America/New_York"));
return objectMapper;
}
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.timeZone("America/New_York");
return builder;
}
}
Entity:
@Entity
public class AllocationEstimate extends AbstractEntity<Integer> {
private static final long serialVersionUID = 1L;
@NotNull(message = "validation.mandatoryField")
@Column(nullable = false)
@Temporal(TemporalType.DATE)
@JsonFormat(pattern = "MM/dd/yyyy")
private Date endDate;
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
}
Controller:
@RestController
@RequestMapping("/allocation-estimate")
public class AllocationEstimateRestController extends AbstractGenericCrudRestController<AllocationEstimate, Integer> {
private final AllocationEstimateService allocationEstimateService;
@Autowired
public AllocationEstimateRestController(AllocationEstimateService allocationEstimateService) {
this.AllocationEstimateService = allocationEstimateService;
}
@Override
@Post
public AllocationEstimate createOrUpdate(@RequestBody @Valid AllocationEstimate resource) {
return super.createOrUpdate(resource);
}
}
I have used all of the above and a combination of different settings. Some with only configureMessageConverters, or only trying to set a @Bean.
My question is if anyone has seen this behavior and how can I fix it? I've tried a combination of different things but my settings are always overridden by the end of the bootup process. I am using Spring 4.3.16.
For Reference:
Configuring ObjectMapper in Spring
https://dzone.com/articles/latest-jackson-integration
How to force Jackson2ObjectMapperBuilder in spring-boot?
SpringMVC Jackson2HttpMessageConverter customization doesn't work
EDIT I'm suspecting that AllEncompassingFormHttpMessageConverter is the culprit, I am not sure what it is but something is instantiating this class multiple times which in turn instantiates MappingJackson2HttpMessageConverter multiple times and thus overriding my converter that I have in my config.