Java 扩展形式的Rest响应日期。一丝不挂
我已将com.faster.jackson版本更新为2.8.10。 之后的日期如下Java 扩展形式的Rest响应日期。一丝不挂,java,spring,spring-boot,jackson,Java,Spring,Spring Boot,Jackson,我已将com.faster.jackson版本更新为2.8.10。 之后的日期如下 "offset": { "totalSeconds": 19800, "id": "+05:30", "rules": { "fixedOffset": true, "transitions": [], "transitionRules": [] } },
"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_添加为_TIMESTAMPS=false。
同样的事情也在发生
模范班
public class VoucherResponseDTO implements Serializable {
private ZonedDateTime createdDate;
//getters & setters
}
在jackson版本更新之前,输出是这样的:“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);
}
}
你能帮我把日期记成字符串吗
感谢并将视为一种解决方法,您可以使用
class Bean {
@JsonSerialize(using = JSR310DateTimeSerializer.class)
public ZonedDateTime dateTime;
}
当然,这个解决方案不能扩展。
如果您在模型类中使用了许多
java.time
组件,那么您不希望用每个字段的特定序列化器类来注释每个字段。您的模型类如何。显示您是否在spring boot中覆盖默认的自动可配置MVC流?e、 g.通过扩展WebMVCConfigureETCNO。我不是。作为添加新依赖项的一部分,必须将com.fasterxml.jackson从2.7.3更改为2.8.10。这是我唯一做的改变。看看这个,我有这样的课。