SpringBoot默认使用的 json 解析框架是 jackson 框架
jackson 解析框架在解析实体类里面是Date数据类型的数据时的默认格式是:UTC 类型,即 yyyy-MM-dd’T’HH:mm:ss.SSS 并且默认为+8时区,即时间基础上加8小时
方式一:全局统一配置在SpringBoot项目yml解决时间问题
spring:
profiles:
active: dev
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
locale: zh_CN
# 日期是否转换为 timestamps
serialization:
write-dates-as-timestamps: false
# 将所有的number类型转为 String 返回
generator:
write-numbers-as-strings: true
方式二:在返回的字段加上注解
(1)返回前端数字太长精度丢失问题解决方法
- 如果使用的是
fastjson
直接在属性上添加:@JSONField(serializeUsing= ToStringSerializer.class)
- 如果使用的是
jackson
直接在属性上添加:@JsonSerialize(using = ToStringSerializer.class)
(2)字段属性时间格式化:
- 如果使用的是
fastjson
,属性上添加:@JSONField(format = "yyyy-MM-dd HH:mm:ss")
- 如果使用的是jackson,属性上添加:
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8"),shape = JsonFormat.Shape.STRING