添加依赖包
1 2 3 4 5 6 | <!-- Jackson进行JSON序列化/反序列化添加Java 8的日期和时间库支持 --> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.13.0</version> </dependency> |
方式一: 单个实体类
1 2 3 4 5 6 7 8 9 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;<br>import com.fasterxml.jackson.databind.annotation.JsonSerialize;<br>import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;<br>import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;<br><br> @JsonFormat(pattern = "HH:mm:ss" ,timezone = "GMT+8" , shape = JsonFormat.Shape.STRING) @JsonDeserialize( using = LocalTimeDeserializer. class ) // 反序列化 @JsonSerialize( using = LocalTimeSerializer. class ) // 序列化 private LocalTime startTime; @JsonFormat(pattern = "HH:mm:ss" ,timezone = "GMT+8" , shape = JsonFormat.Shape.STRING) @JsonDeserialize( using = LocalTimeDeserializer. class ) // 反序列化 @JsonSerialize( using = LocalTimeSerializer. class ) // 序列化 private LocalTime endTime; |
方式二: 全局注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /** * Copyright (c) 2018 储能系统 All rights reserved. * * http://www.avcon.com.cn * * 版权所有,侵权必究! */ package com.avcon.modules.security.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.avcon.common.utils.DateUtils; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.ResourceHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.text.SimpleDateFormat; import java.util.List; import java.util.TimeZone; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Bean public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper mapper = new ObjectMapper(); //日期格式转换 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false ); mapper.setDateFormat( new SimpleDateFormat(DateUtils.DATE_TIME_PATTERN)); mapper.setTimeZone(TimeZone.getTimeZone( "GMT+8" )); //Long类型转String类型 JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(Long. class , ToStringSerializer.instance); javaTimeModule.addSerializer(Long.TYPE, ToStringSerializer.instance); mapper.registerModule(javaTimeModule); converter.setObjectMapper(mapper); return converter; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2022-03-22 Vue 局部全局focus指令
2022-03-22 VUE 跳转百度地图, 腾讯坐标拾取经纬度
2018-03-22 Python 常用内置函数
2018-03-22 Java Graphics 2D绘制图片 在Liunx上乱码