spring.jackson.date-format 这个属性在spring boot 1.x中有用,但是当你项目升级到spring boot2.x
以后,时间格式化变无效了

网上说的原因分析:
               spring boot2 中推荐使用的拦截器为 WebMvcConfigurationSupport 
               spring boot1.x 中使用的是 WebMvcConfigurerAdapter ,但是在添加拦截器并继承
WebMvcConfigurationSupport后会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration
的配置!从而导致所有的Date`返回都变成时间戳!

        网上很多方案说弃用 WebMvcConfigurationSupport 改用 implements WebMvcConfigurer
 这种方案

        其实集成WebMvcConfigurationSupport  以后,直接实现 extendMessageConverters
方法即可,代码如下
@Configuration public class WebMvcConfigurer extends
WebMvcConfigurationSupport { @Value("${spring.jackson.date-format}") private
String dateFormatPattern; @Value("${spring.jackson.time-zone}") private String
timeZone; @Override protected void
extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new
MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper =
converter.getObjectMapper(); // 生成JSON时,将所有Long转换成String SimpleModule
simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class,
ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE,
ToStringSerializer.instance); objectMapper.registerModule(simpleModule); //
时间格式化 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false); //这个可以引用spring boot yml 里的格式化配置和时区配置 objectMapper.setDateFormat(new
SimpleDateFormat(dateFormatPattern));
objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone)); // 设置格式化内容
converter.setObjectMapper(objectMapper); converters.add(0, converter);
super.extendMessageConverters(converters); } }
 

技术
今日推荐
PPT
阅读数 126
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信