我已将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);
}
}
你能帮忙把日期作为一个字符串
谢谢&问候
答案 0 :(得分:0)
作为解决方法,您可以使用
注释相关字段class Bean {
@JsonSerialize(using = JSR310DateTimeSerializer.class)
public ZonedDateTime dateTime;
}
当然,此解决方案无法扩展。
如果您在模型类中使用了许多java.time
组件,则不希望使用它的特定序列化程序类对每个字段进行注释。