RestTemplate使用jackson序列化的时区问题

2018-11-06  本文已影响382人  fantuanjiaozi

问题描述

Springboot项目A服务使用restTemplate.postForObject()调用B服务的rest接口,返回的对象内有一个date类型字段,发现date类型比实际时间小了8个小时。


jackson.jpeg

定位问题过程

JacksonAutoConfiguration设置时区的代码如下:

            private void configureDateFormat(Jackson2ObjectMapperBuilder builder) {
                // We support a fully qualified class name extending DateFormat or a date
                // pattern string value
                String dateFormat = this.jacksonProperties.getDateFormat();
                if (dateFormat != null) {
                    try {
                        Class<?> dateFormatClass = ClassUtils.forName(dateFormat, null);
                        builder.dateFormat(
                                (DateFormat) BeanUtils.instantiateClass(dateFormatClass));
                    }
                    catch (ClassNotFoundException ex) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                                dateFormat);
                        // Since Jackson 2.6.3 we always need to set a TimeZone (see
                        // gh-4170). If none in our properties fallback to the Jackson's
                        // ***先读取配置文件的时区
                        TimeZone timeZone = this.jacksonProperties.getTimeZone();
                        //***如果配置文件没指定时区,则取下面值
                        if (timeZone == null) {
                            timeZone = new ObjectMapper().getSerializationConfig()
                                    .getTimeZone();
                        }
                        simpleDateFormat.setTimeZone(timeZone);
                        builder.dateFormat(simpleDateFormat);
                    }
                }
            }
    private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC");

解决办法

在A服务的配置文件application.properties增加配置,指定jackson使用的时区:

#指定时区为东八区
spring.jackson.time-zone=GMT+8

重新打包部署后OK。

上一篇 下一篇

猜你喜欢

热点阅读