前端参数转带有LocalDateTime实体类,时间少8小时或者无法序列化问题
直接看解决办法:
- 添加时间格式处理
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
- 若无法序列化成功,添加序列化器和反序列化器
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
- 注意要有相关依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
- 效果
// 鉴定时间
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@Column(name = "appraisal_date", nullable = false)
private LocalDateTime appraisalDate;
5.前端传参格式保持一致
// 统一时间格式 yyyy-MM-dd hh:mm:ss, 主要作前端快速提示和后端pojo序列化格式对应,否则LocalDateTime无法序列化成功
if (this.form.appraisalDate !== '' || this.form.appraisalDate !== undefined) {
let dateStr = formatDate(new Date(this.form.appraisalDate),'yyyy-MM-dd hh:mm:ss');
if (dateStr === '1970-01-01 08:00:00') {
this.$message.warning('考试日期不能为空');
return;
}
this.form.appraisalDate = dateStr;
}
console.log(this.form)
以上配置即可解决。