【问题标题】:Rest Response Date in expanded form. Not a String扩展形式的休息响应日期。不是字符串
【发布时间】:2018-11-14 16:37:52
【问题描述】:

我已将 com.faster.jackson 版本更新到 2.8.10。 之后日期如下。

    "offset": {
        "totalSeconds": 19800,
        "id": "+05:30",
        "rules": {
            "fixedOffset": true,
            "transitions": [],
            "transitionRules": []
        }
    },
    "zone": {
        "id": "Asia/Kolkata",
        "rules": {
            "fixedOffset": false,
            "transitions": [
                {
                    "offsetBefore": {
                        "totalSeconds": 21208,
                        "id": "+05:53:28",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "offsetAfter": {
                        "totalSeconds": 21200,
                        "id": "+05:53:20",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "duration": {
                        "seconds": -8,
                        "units": [
                            "SECONDS",
                            "NANOS"
                        ],
                        "zero": false,
                        "negative": true,
                        "nano": 0
                    },
                    "gap": false,
                    "overlap": true,
                    "dateTimeAfter": {
                        "monthValue": 6,
                        "year": 1854,
                        "month": "JUNE",
                        "dayOfMonth": 27,
                        "dayOfWeek": "TUESDAY",
                        "dayOfYear": 178,
                        "hour": 23,
                        "minute": 59,
                        "second": 52,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "dateTimeBefore": {
                        "monthValue": 6,
                        "year": 1854,
                        "month": "JUNE",
                        "dayOfMonth": 28,
                        "dayOfWeek": "WEDNESDAY",
                        "dayOfYear": 179,
                        "hour": 0,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "instant": {
                        "nano": 0,
                        "epochSecond": -3645237208
                    }
                },
                {
                    "offsetBefore": {
                        "totalSeconds": 21200,
                        "id": "+05:53:20",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "offsetAfter": {
                        "totalSeconds": 19270,
                        "id": "+05:21:10",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "duration": {
                        "seconds": -1930,
                        "units": [
                            "SECONDS",
                            "NANOS"
                        ],
                        "zero": false,
                        "negative": true,
                        "nano": 0
                    },
                    "gap": false,
                    "overlap": true,
                    "dateTimeAfter": {
                        "monthValue": 12,
                        "year": 1869,
                        "month": "DECEMBER",
                        "dayOfMonth": 31,
                        "dayOfWeek": "FRIDAY",
                        "dayOfYear": 365,
                        "hour": 23,
                        "minute": 27,
                        "second": 50,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "dateTimeBefore": {
                        "monthValue": 1,
                        "year": 1870,
                        "month": "JANUARY",
                        "dayOfMonth": 1,
                        "dayOfWeek": "SATURDAY",
                        "dayOfYear": 1,
                        "hour": 0,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "instant": {
                        "nano": 0,
                        "epochSecond": -3155694800
                    }
                },
                {
                    "offsetBefore": {
                        "totalSeconds": 19270,
                        "id": "+05:21:10",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "offsetAfter": {
                        "totalSeconds": 19800,
                        "id": "+05:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "duration": {
                        "seconds": 530,
                        "units": [
                            "SECONDS",
                            "NANOS"
                        ],
                        "zero": false,
                        "negative": false,
                        "nano": 0
                    },
                    "gap": true,
                    "overlap": false,
                    "dateTimeAfter": {
                        "monthValue": 1,
                        "year": 1906,
                        "month": "JANUARY",
                        "dayOfMonth": 1,
                        "dayOfWeek": "MONDAY",
                        "dayOfYear": 1,
                        "hour": 0,
                        "minute": 8,
                        "second": 50,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "dateTimeBefore": {
                        "monthValue": 1,
                        "year": 1906,
                        "month": "JANUARY",
                        "dayOfMonth": 1,
                        "dayOfWeek": "MONDAY",
                        "dayOfYear": 1,
                        "hour": 0,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "instant": {
                        "nano": 0,
                        "epochSecond": -2019705670
                    }
                },
                {
                    "offsetBefore": {
                        "totalSeconds": 19800,
                        "id": "+05:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "offsetAfter": {
                        "totalSeconds": 23400,
                        "id": "+06:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "duration": {
                        "seconds": 3600,
                        "units": [
                            "SECONDS",
                            "NANOS"
                        ],
                        "zero": false,
                        "negative": false,
                        "nano": 0
                    },
                    "gap": true,
                    "overlap": false,
                    "dateTimeAfter": {
                        "monthValue": 10,
                        "year": 1941,
                        "month": "OCTOBER",
                        "dayOfMonth": 1,
                        "dayOfWeek": "WEDNESDAY",
                        "dayOfYear": 274,
                        "hour": 1,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "dateTimeBefore": {
                        "monthValue": 10,
                        "year": 1941,
                        "month": "OCTOBER",
                        "dayOfMonth": 1,
                        "dayOfWeek": "WEDNESDAY",
                        "dayOfYear": 274,
                        "hour": 0,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "instant": {
                        "nano": 0,
                        "epochSecond": -891581400
                    }
                },
                {
                    "offsetBefore": {
                        "totalSeconds": 23400,
                        "id": "+06:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "offsetAfter": {
                        "totalSeconds": 19800,
                        "id": "+05:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "duration": {
                        "seconds": -3600,
                        "units": [
                            "SECONDS",
                            "NANOS"
                        ],
                        "zero": false,
                        "negative": true,
                        "nano": 0
                    },
                    "gap": false,
                    "overlap": true,
                    "dateTimeAfter": {
                        "monthValue": 5,
                        "year": 1942,
                        "month": "MAY",
                        "dayOfMonth": 14,
                        "dayOfWeek": "THURSDAY",
                        "dayOfYear": 134,
                        "hour": 23,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "dateTimeBefore": {
                        "monthValue": 5,
                        "year": 1942,
                        "month": "MAY",
                        "dayOfMonth": 15,
                        "dayOfWeek": "FRIDAY",
                        "dayOfYear": 135,
                        "hour": 0,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "instant": {
                        "nano": 0,
                        "epochSecond": -872058600
                    }
                },
                {
                    "offsetBefore": {
                        "totalSeconds": 19800,
                        "id": "+05:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "offsetAfter": {
                        "totalSeconds": 23400,
                        "id": "+06:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "duration": {
                        "seconds": 3600,
                        "units": [
                            "SECONDS",
                            "NANOS"
                        ],
                        "zero": false,
                        "negative": false,
                        "nano": 0
                    },
                    "gap": true,
                    "overlap": false,
                    "dateTimeAfter": {
                        "monthValue": 9,
                        "year": 1942,
                        "month": "SEPTEMBER",
                        "dayOfMonth": 1,
                        "dayOfWeek": "TUESDAY",
                        "dayOfYear": 244,
                        "hour": 1,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "dateTimeBefore": {
                        "monthValue": 9,
                        "year": 1942,
                        "month": "SEPTEMBER",
                        "dayOfMonth": 1,
                        "dayOfWeek": "TUESDAY",
                        "dayOfYear": 244,
                        "hour": 0,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "instant": {
                        "nano": 0,
                        "epochSecond": -862637400
                    }
                },
                {
                    "offsetBefore": {
                        "totalSeconds": 23400,
                        "id": "+06:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "offsetAfter": {
                        "totalSeconds": 19800,
                        "id": "+05:30",
                        "rules": {
                            "fixedOffset": true,
                            "transitions": [],
                            "transitionRules": []
                        }
                    },
                    "duration": {
                        "seconds": -3600,
                        "units": [
                            "SECONDS",
                            "NANOS"
                        ],
                        "zero": false,
                        "negative": true,
                        "nano": 0
                    },
                    "gap": false,
                    "overlap": true,
                    "dateTimeAfter": {
                        "monthValue": 10,
                        "year": 1945,
                        "month": "OCTOBER",
                        "dayOfMonth": 14,
                        "dayOfWeek": "SUNDAY",
                        "dayOfYear": 287,
                        "hour": 23,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "dateTimeBefore": {
                        "monthValue": 10,
                        "year": 1945,
                        "month": "OCTOBER",
                        "dayOfMonth": 15,
                        "dayOfWeek": "MONDAY",
                        "dayOfYear": 288,
                        "hour": 0,
                        "minute": 0,
                        "second": 0,
                        "nano": 0,
                        "chronology": {
                            "calendarType": "iso8601",
                            "id": "ISO"
                        }
                    },
                    "instant": {
                        "nano": 0,
                        "epochSecond": -764145000
                    }
                }
            ],
            "transitionRules": []
        }
    },
    "monthValue": 6,
    "year": 2018,
    "month": "JUNE",
    "dayOfMonth": 5,
    "dayOfWeek": "TUESDAY",
    "dayOfYear": 156,
    "hour": 14,
    "minute": 10,
    "second": 21,
    "nano": 534000000,
    "chronology": {
        "calendarType": "iso8601",
        "id": "ISO"
    }
}

预期格式为“createdDate”:“2018-06-05T08:17:14.010Z”

我尝试添加 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false。 还是一样。

模型类

public class VoucherResponseDTO implements Serializable {

private ZonedDateTime createdDate;
//getters & setters

}

在杰克逊版本更新输出之前是这样的:“2018-06-05T08:17:14.010Z”

我在下面有一堂课。

 @Configuration
public class JacksonConfiguration {

    @Bean
    Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
        JavaTimeModule module = new JavaTimeModule();
        module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE);
        module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE);
        module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE);
        return new Jackson2ObjectMapperBuilder()
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .findModulesViaServiceLoader(true)
                .modulesToInstall(module);
    }
}

请您帮忙将日期作为字符串获取

感谢和问候

【问题讨论】:

  • 你的模型课怎么样。表明
  • 你是否覆盖了 spring-boot 中默认的自动配置 MVC 流?例如通过扩展 WebMvcConfigurer 等
  • 没有。我不是。作为添加新依赖的一部分,必须将 com.fasterxml.jackson 从 2.7.3 更改为 2.8.10。这是我所做的唯一改变。
  • 我有这样的课。

标签: java spring spring-boot jackson


【解决方案1】:

作为一种变通方法,您可以使用注释相关字段

class Bean {
     @JsonSerialize(using = JSR310DateTimeSerializer.class)
     public ZonedDateTime dateTime;
}

当然,该解决方案无法扩展。 如果您在模型类中使用了许多 java.time 组件,您不希望使用其特定的序列化程序类来注释每个字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多