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并采用前后端分离的企业级微服务敏捷开发系统架构.并引入组件化的思想实现高内聚低耦合,项目代码简洁注释丰富上手 ...
随机推荐
- [原创]Cadence Allegro16.6安装
选择Cancel. lisence managner安装完成,然后安装Product installation Cadence OrCad Allegro SPB 16.6 完整版+和谐文件 链接:h ...
- vue-router 去掉#
vue-router默认的路由模式是hash,我们要去掉url中的#需要将路由模式切换为history const router = new VueRouter({ mode: 'history', ...
- centos6.9关闭防火墙
/etc/init.d/iptables stop 临时关闭防火墙, chkconfig iptables off 永久关闭防火墙 查看防火墙状态 chkconfig --list i ...
- RabbitMQ原理图
一.RabbitMQ 原理图 二.Rabbit 交换器讲解 1 Direct 交换器(发布与订阅 完全匹配) 1.2搭建环境 1 ...
- JSP(一):JSP概要
问题: 在学习了 Servlet 之后,使用 Servlet 进行页面的展现,代码书写过于麻烦.极大的影响了开发的效率,那么有没有一种方式可以让我们像以前写网页一样来进行网页的编程工作呢? 解决: 使 ...
- Ajax 的异步调用和批量修改
AJAX的异步调用的分层 有四个jsp页面,在index.jsp页面上 要在dataDiv出显示调用的的数据回显到此处,可以让showStudent2.jsp页面的数据回调到此处,$("#d ...
- Syntax error, insert "}" to complete ClassBody错误解决
Syntax error, insert "}" to complete ClassBody 报该错误是因为我从网页上粘贴了别人的代码,并没有发现什么异常但还是编译器报红叉. 解决 ...
- LeetCode笔记:140. Word Break II
题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...
- python爬虫第五天
cookie 我们访问网页是通过http协议进行的,而http协议是一个无状态协议(无法维持会话之间的状态),比如我们登录一个网站成功后访问另一个网页,那么登录状态 ...
- Java基础-对象与类
面向对象程序设计概述 面向对象的程序设计(简称OOP)时当今主流的程序设计范型,已经取代了"结构化"过程化程序设计开发技术,Java是完全面向对象的. 类 类设计构造对象的模板或蓝 ...