解决黑马点评项目中实现商铺缓存与数据库的双写一致问题出现的问题:时间问题
2023-01-10 10:41:48.177 ERROR 1796 — [nio-8081-exec-2] com.hmdp.config.WebExceptionAdvice : org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: raw timestamp (1642069999999) not allowed for java.time.LocalDateTime
: need additional information such as an offset or time-zone (see class Javadocs); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: raw timestamp (1642069999999) not allowed for java.time.LocalDateTime
: need additional information such as an offset or time-zone (see class Javadocs)
at [Source: (PushbackInputStream); line: 9, column: 17] (through reference chain: com.hmdp.entity.Shop[“updateTime”])
Java的LocalDateTime 转为 前端的时间戳
/**
* 由于 LocalDateTime 类型在转换 JSON 的时候,并不能被转换为字符串,使用 @JsonFormat 只能转换为指定的 pattern 类型,因此我们需要自定义一个序列化执行器
* LocalDateTime 序列化(将 LocalDateTime类型 转换为 时间戳 返回给前端 )
*
*/
public class LocalDateTimeSerializer implements ObjectSerializer {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
if (object != null) {
LocalDateTime localDateTime = (LocalDateTime) object;
//将localDateTime转换为中国区(+8)时间戳。
serializer.write(localDateTime.toInstant(ZoneOffset.ofHours(8)).to