- private static ObjectMapper createObjectMapper() {
- ObjectMapper objectMapper = new ObjectMapper();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- objectMapper.setDateFormat(df);
- return objectMapper;
- }
@StaxMan: I am a bit concerned if ObjectMapper is still thread-safe after ObjectMapper#setDateFormat() is called. It is known that SimpleDateFormat is not thread safe, thus ObjectMapper won't be unless it clones e.g. SerializationConfig before each writeValue() (I doubt). Could you debunk my fear? – dma_k Aug 2, 2013 at 12:09
DateFormat is indeed cloned under the hood. Good suspicion there, but you are covered. – StaxMan Aug 2, 2013 at 19:433. 修改 SpringBoot 配置增加配置 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss, 这种配置, 只对 Spring BeanFactory 中创建的 Jackson ObjectMapper有效, 例如 HTTP 请求和响应中对 Date 类型的转换
- spring:
- ...
- jackson:
- date-format: yyyy-MM-dd HH:mm:ss
这样统一存在的隐患是丢失毫秒部分. 这个问题业务人员基本上是不会关心的. 如果需要, 就在格式中加上.
第一是 Spring 配置, 这样所有的请求响应都统一了
- spring:
- ...
- jackson:
- date-format: yyyy-MM-dd HH:mm:ss