springboot中如何配置LocalDateTime JSON返回时间戳
springboot配置LocalDateTime JSON返回时间戳
方案一
创建配置类
注意:使用这种方式,所有Controller的JSON数据返回,只要是LocalDateTime类型都会被转成时间戳
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.io.IOException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; /** * @author jamesluozhiwei * @date 2019/12/18 */ @Configuration public class LocalDateTimeSerializerConfig { /** * 序列化LocalDateTime * @return */ @Bean @Primary public ObjectMapper serializingObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); objectMapper.registerModule(javaTimeModule); return objectMapper; } /** * 序列化实现 */ public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { if (value != null){ long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); gen.writeNumber(timestamp); } } } /** * 反序列化实现 */ public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { long timestamp = p.getValueAsLong(); if (timestamp > 0){ return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),ZoneId.systemDefault()); }else{ return null; } } } }
方案二
和方案一 一样都是实现序列化和反序列化接口
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.io.IOException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; /** * @author jamesluozhiwei * @date 2019/12/18 */ @Configuration public class LocalDateTimeSerializerConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> { builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer()); builder.deserializerByType(LocalDateTime.class,new LocalDateTimeDeserializer()); }; } /** * 序列化 */ public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { if (value != null){ long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); gen.writeNumber(timestamp); } } } /** * 反序列化 */ public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { long timestamp = p.getValueAsLong(); if (timestamp > 0){ return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),ZoneId.systemDefault()); }else{ return null; } } } }
springboot中的时间以Json返回格式不正确的解决
如果后端数据库存储时间类型的数据使用了datetime类型,那么后端查出来的数据就是LocalDateTime类型,此时若以Json传给前端,那么前端接收到的时间里就会有“T”,此时只要在传参上加上标签即可:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;
而如果传给前端的是LocalDate的时间类型,前端接收到的时间也是不正常的,此时需要加上标签处理:
@JsonFormat(pattern = "yyyy-MM-dd") private LocalDate bizTime;
如果以上方法还是不能解决问题,可尝试使用:
@JSONField(format="yyyy-MM-dd HH:mm:ss")
总结
如果你有更好的方法,可以相互讨论学习一下~
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
java 使用ConcurrentHashMap和计数器实现锁
这篇文章主要介绍了java 使用ConcurrentHashMap和计数器实现锁的相关资料,需要的朋友可以参考下2017-05-05IntelliJ IDEA 安装教程2019.09.23(最新版)
本文通过图文并茂的形式给大家介绍了IntelliJ IDEA 安装教程2019.09.23最新版,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下2019-10-10Spring Boot集成spring-boot-devtools开发时实现热部署的方式
这篇文章主要介绍了Spring Boot集成spring-boot-devtools开发时实现热部署的方式,文中还给大家提到了spring boot 实现热部署的方式及集成注意事项,感兴趣的朋友跟随脚本之家小编一起学习吧2018-05-05LocalDateTime日期时间格式中间多了一个T的问题及解决
这篇文章主要介绍了LocalDateTime日期时间格式中间多了一个T的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-03-03
最新评论