当前位置: 首页 > news >正文

自定义反序列化类将LocalDate时间格式转为 LocalDateTime

从前端接收数据反序列化成类,如果时间格式不一致可能会反序列化失败

public class StorageDTO implements Serializable {private static final long serialVersionUID = 1L;......//实体类中格式为@JsonFormat(pattern = "yyyy-MM-dd")@JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)@ApiModelProperty("产生时间")private LocalDateTime generateTime;}
//自定义反序列化类
package com......config;import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;public class CustomLocalDateTimeDeserializer extends LocalDateTimeDeserializer {private static final long serialVersionUID = 1L;private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;private CustomLocalDateTimeDeserializer() {super(DEFAULT_FORMATTER);}public CustomLocalDateTimeDeserializer(DateTimeFormatter formatter) {super(formatter);}@Overrideprotected LocalDateTimeDeserializer withDateFormat(DateTimeFormatter formatter) {return new CustomLocalDateTimeDeserializer(formatter);}@Overridepublic LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {LocalDate localDate = new LocalDateDeserializer(_formatter).deserialize(p, ctxt);LocalDateTime localDateTime = localDate.atStartOfDay();return localDateTime;}
}

后端统一返回格式化后的日期

package com.......config;import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.......filters.ReHttpServletRequestFilter;
import com.......interceptor.DeviceHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.TimeZone;/*** 通用配置** @author balance*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";//默认日期时间格式public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";//默认日期格式public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";//默认时间格式@Autowiredprivate DeviceHandlerInterceptor deviceHandlerInterceptor;/*** 自定义拦截规则*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(deviceHandlerInterceptor).addPathPatterns("/test/**").excludePathPatterns("/testPage/**");//设备端请求拦截器}@Beanpublic FilterRegistrationBean httpServletRequestReplacedRegistration() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new ReHttpServletRequestFilter());registration.addUrlPatterns("/*");registration.addInitParameter("paramName", "paramValue");registration.setName("reHttpServletRequestFilter");registration.setOrder(1);return registration;}/*** 跨域配置*/@Beanpublic CorsFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true);// 设置访问源地址config.addAllowedOriginPattern("*");// 设置访问源请求头config.addAllowedHeader("*");// 设置访问源请求方法config.addAllowedMethod("*");// 有效期 1800秒config.setMaxAge(1800L);// 添加映射路径,拦截一切请求UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", config);// 返回新的CorsFilterreturn new CorsFilter(source);}/*** 配置全局localDateTime输出格式* @return*/@Beanpublic MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();ObjectMapper objectMapper = new ObjectMapper();objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 忽略json字符串中不识别的属性objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);// 忽略无法转换的对象objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); // PrettyPrinter 格式化输出objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);// NULL不参与序列化objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));// 指定时区objectMapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT)); // 日期类型字符串处理// java8日期处理JavaTimeModule javaTimeModule = new JavaTimeModule();//序列化规则javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));//反序列化规则javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));objectMapper.registerModule(javaTimeModule);mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);return mappingJackson2HttpMessageConverter;}@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {converters.add(mappingJackson2HttpMessageConverter());}}

相关文章:

自定义反序列化类将LocalDate时间格式转为 LocalDateTime

从前端接收数据反序列化成类,如果时间格式不一致可能会反序列化失败 public class StorageDTO implements Serializable {private static final long serialVersionUID 1L;......//实体类中格式为JsonFormat(pattern "yyyy-MM-dd")JsonDeserialize(using CustomL…...

MySQL JSON_TABLE() 函数

JSON_TABLE()函数从一个指定的JSON文档中提取数据并返回一个具有指定列的关系表。 应用&#xff1a;数据库字段以JSON 存储后&#xff0c;实际应用需要对其中一个字段进行查询 语法 JSON_TABLE(json,path COLUMNS(column[,column[,...]]))column:name参数 json必需的。一个 …...

【MATLAB第80期】基于MATLAB的结构核岭回归SKRR多输入单输出回归预测及分类预测模型

【MATLAB第80期】基于MATLAB的结构核岭回归SKRR多输入单输出回归预测及分类预测模型 SKRR这是Gustau Camps-Valls等人在“用深度结构核回归检索物理参数”中提出的结构核岭回归&#xff08;SKRR&#xff09;方法。 参考文献&#xff1a; Camps-Valls,Retrieval of Physical Pa…...

Qt消息对话框的使用

本文介绍Qt消息对话框的使用。 QMessageBox类是Qt编程中常用到的一个类&#xff0c;主要用来进行一些简单的消息提示&#xff0c;比如&#xff1a;问题对话框&#xff0c;信息对话框等&#xff0c;这些对话框都属于QMessageBox类的静态方法&#xff0c;使用起来比较简单&#…...

spring的Ioc、DI以及Bean的理解

文章目录 什么是Ioc&#xff1f;Spring和这有什么关系Spring是怎么做的&#xff1f;如果service层对dao层有依赖该怎么办&#xff1f;什么叫做依赖注入Spring这样做的目的是什么参考文献 什么是Ioc&#xff1f; Ioc(Inversion of Control) 控制反转&#xff0c;就是使用对象时…...

倒计时 天时分秒

shijian() {const EndTIME new Date(开始时间变量); // 截止时间const NowTime new Date(); // 开始时间const usedTime EndTIME - NowTime; // 相差的毫秒数const days Math.floor(usedTime / (24 * 3600 * 1000)); // 计算出天数const leavel usedTime % (24 * 3600 * 1…...

Spring篇---第六篇

系列文章目录 文章目录 系列文章目录一、Spring 框架中的单例 Bean 是线程安全的么?二、Spring 是怎么解决循环依赖的?三、说说事务的隔离级别一、Spring 框架中的单例 Bean 是线程安全的么? Spring 框架并没有对单例 Bean 进行任何多线程的封装处理。 关于单例 Bean 的线程…...

【unity小技巧】适用于任何 2d 游戏的钥匙门系统和buff系统——UnityEvent的使用

文章目录 每篇一句前言开启配置门的开启动画代码调用&#xff0c;控制开启门动画 新增CollisionDetector 脚本&#xff0c;使用UnityEvent &#xff0c;控制钥匙和门的绑定多把钥匙控制多个门一把钥匙控制多个门 BUFF系统扩展参考源码完结 每篇一句 人总是害怕去追求自己最重要…...

爬虫ip如何加入到代码里实现自动化数据抓取

以下是一个使用HTTP:Tiny和www.weibo.com的音频爬虫程序的示例。这个示例使用了https://www.duoip.cn/get_proxy来获取爬虫IP。请注意&#xff0c;这个示例可能需要根据你的实际需求进行调整。 #!/usr/bin/perluse strict; use warnings; use HTTP::Tiny; use LWP::UserAgent; …...

在win10上安装配置Hadoop的环境变量

一、背景 在windows10系统中运行seatunnel 二、安装部署 2.1. 下载 Hadoop包 从 Apache Hadoop 官网下载最新版本的 Hadoop&#xff0c;版本号保持与服务端的Hadoop版本一致。 https://hadoop.apache.org/releases.htmlIndex of /apache/hadoop/core/hadoop-3.2.3/ 2.2. 解…...

MAX插件CG Magic怎么云渲染?操作方法已整起!

小编这里会收到不少网友的反馈是关于3ds max插件CG Magic怎么云渲染&#xff1f; 3d max的这款插件CG MAGIC的出现就是为了设计师使用过程中&#xff0c;可以省时又省心的完成工作。 同时&#xff0c;大家要了键下&#xff0c;现阶段CG MAGIC有18个板块&#xff0c;118个模块…...

尝试使用jmeter-maven-plugin

前提准备 1、maven项目 2、已安装JMeter、Jenkins、maven、jdk 环境要求&#xff1a; jmeter>5.6.2 maven >3.9 jdk>1.8 Jenkins ? 备注&#xff1a;jmeter-maven-plugin 无需下载&#xff0c;可查阅相关地址&#xff1a;GitHub - jmeter-maven-plugin/jmete…...

navigator.userAgent.toLowerCase区分设备,浏览器

navigator.userAgent.toLowerCase区分设备&#xff0c;浏览器 navigator.userAgent.toLowerCase(&#xff09;区分设备是pc还是移动端在确认是移动端的基础上&#xff0c;判断是Android、ipad、iphone内置的浏览器&#xff0c;比如新浪微博、腾讯QQ&#xff08;非QQ浏览器&…...

防火墙操作:开放端口ICMP时间戳请求漏洞修复

响应ICMP时间戳请求漏洞修复 firewall-cmd --permanent --zonepublic --add-icmp-blocktimestamp-reply firewall-cmd --permanent --zonepublic --add-icmp-blocktimestamp-request firewall-cmd --reload --防火墙状态 systemctl status firewalld --打开防火墙 systemctl …...

MySQL配置环境变量和启动登录

如果不配置环境变量&#xff0c;每次登录 MySQL 服务器时就必须进入到 MySQL 的 bin 目录下&#xff0c;也就是输入“cd C:\Program Files\MySQL\MySQL Server 5.7\bin”命令后&#xff0c;才能使用 MySQL 等其它命令工具&#xff0c;这样比较麻烦。配置环境变量以后就可以在任…...

救济金发放(The Dole Queue, UVa 133)rust解法

n(n<20)个人站成一圈&#xff0c;逆时针编号为1&#xff5e;n。有两个官员&#xff0c;A从1开始逆时针数&#xff0c;B从n开始顺时针数。在每一轮中&#xff0c;官员A数k个就停下来&#xff0c;官员B数m个就停下来&#xff08;注意有可能两个官员停在同一个人上&#xff09;…...

oracle实验四

创建文件 &#xff08;1&#xff09;为 ORCL 数据库创建一个名为 BOOKTBS1 的永久性表空间&#xff0c;数据文件为’d:\bt01.dbf’ &#xff0c;大小为100M&#xff0c;区采用自动扩展方式&#xff08;即自动分配&#xff09;&#xff0c; 段采用自动管理方式&#xff1b; &am…...

数据结构-堆排序Java实现

目录 一、引言二、算法步骤三、原理演示步骤1: 构建最大堆步骤2: 交换和堆化步骤3: 排序完成 四、代码实战五、结论 一、引言 堆排序是一种利用堆这种数据结构所设计的一种排序算法。堆是一个近似完全二叉树的结构&#xff0c;并同时满足堆积的性质&#xff1a;即子结点的键值或…...

C#进阶——反射(Reflection)

定义&#xff1a;反射指的是在运行时动态地获取、检查和操作程序中的类型信息&#xff0c;而在我们的Unity中反射允许开发者在运行时通过代码来访问和修改对象的属性、方法和字段&#xff0c;而不需要提前知道这些成员的具体信息。 举一个例子&#xff0c;我们使用反射在运行的…...

Oracle 运维篇+应用容器数据库的install、upgrade、patch、uninstall

★ 知识点 ※ DEFAULT_SHARING参数的取值 METADATA: 元数据链接共享数据库对象的元数据&#xff0c;但其数据对于每个容器是唯一的。这些数据库对象被称为元数据链接的应用程序公共对象。此设置为默认设置。DATA: 数据链接共享数据库对象&#xff0c;其数据对于应用程序容器中…...

面试官:父子线程之间如何共享、传递数据?

面试考察点 ThreadLocal 机制理解&#xff1a;面试官不仅仅是想知道你会不会用 ThreadLocal&#xff0c;更是想知道你是否清楚 ThreadLocal 的数据隔离特性——它只对当前线程可见&#xff0c;子线程天然拿不到父线程的数据。方案演进认知&#xff1a;考察你是否了解从 Thread…...

告别样本失衡:用PyTorch手把手实现Focal Loss,让你的目标检测模型更关注‘难啃的骨头’

用Focal Loss解决目标检测中的样本失衡难题&#xff1a;PyTorch实战指南 当你盯着训练日志里那些"虚高"的准确率指标时&#xff0c;是否注意到模型对小目标、遮挡目标的识别率始终低迷&#xff1f;这很可能不是数据标注的问题&#xff0c;而是经典交叉熵损失函数在面…...

推荐一款创新的滚动视图库:PullScrollView

推荐一款创新的滚动视图库&#xff1a;PullScrollView 【免费下载链接】PullScrollView 1.仿照新浪微博Android客户端个人中心的ScrollView&#xff0c;下拉背景伸缩回弹效果。 2.ScrollView仿IOS回弹效果。 项目地址: https://gitcode.com/gh_mirrors/pu/PullScrollView …...

maven常用命令大全

参考地址&#xff1a; 1.maven常用命令大全(附详细解释)&#xff0c;https://blog.csdn.net/good_good_xiu/article/details/116740333 2.maven常用命令集合&#xff08;收藏大全&#xff09;&#xff0c;https://zhuanlan.zhihu.com/p/355889432 3.Maven查看插件信息&#…...

微信好友关系终极检测方案:WechatRealFriends帮你一键识别单向好友

微信好友关系终极检测方案&#xff1a;WechatRealFriends帮你一键识别单向好友 【免费下载链接】WechatRealFriends 微信好友关系一键检测&#xff0c;基于微信ipad协议&#xff0c;看看有没有朋友偷偷删掉或者拉黑你 项目地址: https://gitcode.com/gh_mirrors/we/WechatRea…...

三星固件下载解密终极指南:Bifrost跨平台解决方案

三星固件下载解密终极指南&#xff1a;Bifrost跨平台解决方案 【免费下载链接】SamloaderKotlin 项目地址: https://gitcode.com/gh_mirrors/sa/SamloaderKotlin 三星设备用户经常面临固件下载和管理的难题——官方服务器限制、复杂的解密流程、跨平台兼容性问题等。Bi…...

你的论文“说人话”,评委才听得进去:好写作AI的答辩PPT,不是“做”出来的,是“翻译”出来的

你有没有经历过这种时刻&#xff1a;论文写了五万字&#xff0c;文章查重过了&#xff0c;盲审也过了&#xff0c;导师说“内容很扎实”&#xff0c;你长舒一口气。然后导师补了一句&#xff1a;“下周答辩&#xff0c;你做个PPT。” 完了。 不是不会做PPT&#xff0c;是不知…...

你的App连不上WiFi?可能是Android 10的隐私权限在搞鬼(附排查指南)

Android 10 WiFi连接失效深度排查指南&#xff1a;隐私权限与API变革解析 最近在调试一个智能家居App时&#xff0c;遇到了一个诡异的问题&#xff1a;在Android 10设备上&#xff0c;WiFi连接功能总是莫名其妙失败&#xff0c;而在旧版本系统却运行良好。这让我意识到&#xf…...

3步掌握Charticulator:从数据到专业图表的免费完整指南

3步掌握Charticulator&#xff1a;从数据到专业图表的免费完整指南 【免费下载链接】charticulator Interactive Layout-Aware Construction of Bespoke Charts 项目地址: https://gitcode.com/gh_mirrors/ch/charticulator 数据可视化不再是程序员的专利&#xff0c;现…...

Kubernetes Pod 状态同步机制

Kubernetes Pod状态同步机制解析 在分布式系统中&#xff0c;容器编排平台Kubernetes通过Pod状态同步机制确保集群资源与实际运行状态的一致性。这一机制不仅保障了应用的高可用性&#xff0c;还为运维人员提供了透明的状态管理能力。本文将深入探讨Pod状态同步的核心逻辑&…...