SpringBoot前后端分离Instant时间戳自定义解析
在SpringBoot项目中,前后端规定传递时间使用时间戳(精度ms).
@Data
public class Incident {
@ApiModelProperty(value = "故障ID", example = "1")
private Integer id;
@ApiModelProperty(value = "故障产生时间", allowEmptyValue = true)
private Instant createdTime;
@ApiModelProperty(value = "故障恢复时间", allowEmptyValue = true)
private Instant recoveryTime;
}
以上为简略实体类定义.
@Transactional(rollbackFor = Exception.class)
@PostMapping(path = "/incident")
public void AddIncident(@Valid @RequestBody Incident incident) {
incident.setBusinessId(0);
if (1 != incidentService.addIncident(incident)) {
throw new Exception("...");
}
}
在实际使用过程中,发现Incident中的createdTime以及recoveryTime数值不对.
排查故障,前端去除时间戳后三位(即ms数),则时间基本吻合.
因此,可以确定是SpringBoot在转换Instant时使用Second进行转换.
因此对于Instant类型的转换添加自定义解析(SpringBoot使用com.fasterxml.jackson解析数据).
注意,.此处需要分别实现序列化(后端返回前端数据)以及反序列化(前端上传数据).
public class InstantJacksonDeserialize extends JsonDeserializer<Instant> {
@Override
public Instant deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String text = jsonParser.getText();
Long aLong = Long.valueOf(text);
Instant res = Instant.ofEpochMilli(aLong);
return res;
}
}
public class InstantJacksonSerializer extends JsonSerializer<Instant> {
@Override
public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeNumber(instant.toEpochMilli());
}
}
在涉及到Instant的属性上加上相应注解,代码具体如下:
@Data
public class Incident {
@ApiModelProperty(value = "故障ID", example = "1")
private Integer id;
@JsonSerialize(using = InstantJacksonSerializer.class)
@JsonDeserialize(using = InstantJacksonDeserialize.class)
@ApiModelProperty(value = "故障产生时间", allowEmptyValue = true)
private Instant createdTime;
@JsonSerialize(using = InstantJacksonSerializer.class)
@JsonDeserialize(using = InstantJacksonDeserialize.class)
@ApiModelProperty(value = "故障恢复时间", allowEmptyValue = true)
private Instant recoveryTime;
}
添加注解后,Instant对象能够按照ms精度进行解析.
PS:
如果您觉得我的文章对您有帮助,可以扫码领取下红包,谢谢!
SpringBoot前后端分离Instant时间戳自定义解析的更多相关文章
- Springboot前后端分离开发
.1.springboot前后端分离开发之前要配置好很多东西,这周会详细补充博客内容和遇到的问题的解析 2,按照下面流程走一遍 此时会加载稍等一下 pom.xml显示中加上阿里云镜像可以加速下载配置文 ...
- vue+springboot前后端分离实现单点登录跨域问题处理
最近在做一个后台管理系统,前端是用时下火热的vue.js,后台是基于springboot的.因为后台系统没有登录功能,但是公司要求统一登录,登录认证统一使用.net项目组的认证系统.那就意味着做单点登 ...
- docker-compose 部署 Vue+SpringBoot 前后端分离项目
一.前言 本文将通过docker-compose来部署前端Vue项目到Nginx中,和运行后端SpringBoot项目 服务器基本环境: CentOS7.3 Dokcer MySQL 二.docker ...
- 基于SpringBoot前后端分离的点餐系统
基于SpringBoot前后端分离的点餐系统 开发环境:主要采用Spring boot框架和小程序开发 项目简介:点餐系统,分成卖家端和买家端.买家端使用微信小程序开发,实现扫码点餐.浏览菜单.下单. ...
- springboot 前后端分离开发 从零到整(三、登录以及登录状态的持续)
今天来写一下怎么登录和维持登录状态. 相信登录验证大家都比较熟悉,在Javaweb中一般保持登录状态都会用session.但如果是前后端分离的话,session的作用就没有那么明显了.对于前后端分离的 ...
- springboot 前后端分离开发 从零到整(一、环境的搭建)
第一次写文章,有什么错误地方请大家指正,也请大家见谅. 这次为大家分享我做毕业设计的一个过程,之前没有接触过springboot,一直做的都是Javaweb和前端,做了几个前后端分离的项目.现在听说s ...
- JEECG-Boot 项目介绍——基于代码生成器的快速开发平台(Springboot前后端分离)
Jeecg-Boot 是一款基于代码生成器的智能开发平台!采用前后端分离架构:SpringBoot,Mybatis,Shiro,JWT,Vue&Ant Design.强大的代码生成器让前端和后 ...
- springboot 前后端分离开发 从零到整(二、邮箱注册)
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: ...
- SpringCloud SpringBoot 前后端分离企业级微服务架构源码赠送
基于SpringBoot2.x.SpringCloud和SpringCloudAlibaba并采用前后端分离的企业级微服务敏捷开发系统架构.并引入组件化的思想实现高内聚低耦合,项目代码简洁注释丰富上手 ...
随机推荐
- 简易promise的实现(二)
code 上一章中我们遇到了两个问题 1.异步调用顺序的问题 2.then返回一个promise的问题 思考 如果控制异步回调的顺序? 因为异步操的时间作我们无法控制,但是我们只需要按顺序执行回调函数 ...
- js原型与原型链探究
原型有一个非常重要的属性叫 prototype 一.先写一个简单的例子,看看 A的原型和A的实例 分别是什么 function A() {} var a = new A() console.log(a ...
- unity 时间转换方式
第一种 计时器的写法 带有调用系统时间 using System; using System.Collections; using System.Collections.Generic; using ...
- 改进SQL Server 性能 - 索引碎片重建
我们先来看一个用户表上的索引碎片情况: DBCC SHOWCONTIG scanning 'Lead' table...Table: 'Lead' (1422628111); index ID: 1, ...
- VSTO 获取sheet单元格行列数
Public Sub Igor() Dim Dtsheet As Excel.Worksheet Dim TotalC As Long '原始数据范围列 Dim TotalR As Long '原始数 ...
- echarts中的区域缩放组件dataZoom,主动触发选区缩放点击事件
options设置 toolbox: { // 工具栏 feature: { dataZoom : { // 选时间缩放功能 show : true, // show为true时,才能触发takeGl ...
- BZOJ.4144.[AMPPZ2014]Petrol(Kruskal重构树)
BZOJ 看别人代码的时候发现哪一步都很眼熟,突然想起来,就在四个月前我好像看过还给别人讲过?mmp=v= 果然不写写就是容易忘.写了好歹忘了的时候还能复习呢(虽然和看别人的好像也没多少差别?). 首 ...
- banner | what is the "banner" ?
banner/横幅 获取 banner(横幅) 信息属于信息搜集 因为在 banner 信息中,可以获取到软件开发商.软件名称.服务类型.版本号等 而版本号有时候就会存在公开的 CVE 问 ...
- Ubuntu下编译SqlCipher以及解密微信数据库EnMicroMsg.db过程和坑
wget https://codeload.github.com/sqlcipher/sqlcipher/zip/v3.4.2 ./configure --enable-tempstore=yes C ...
- Rectangular Covering [POJ2836] [状压DP]
题意 平面上有 n (2 ≤ n ≤ 15) 个点,现用平行于坐标轴的矩形去覆盖所有点,每个矩形至少盖两个点,矩形面积不可为0,求这些矩形的最小面积. Input The input consists ...
