在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时间戳自定义解析的更多相关文章

  1. Springboot前后端分离开发

    .1.springboot前后端分离开发之前要配置好很多东西,这周会详细补充博客内容和遇到的问题的解析 2,按照下面流程走一遍 此时会加载稍等一下 pom.xml显示中加上阿里云镜像可以加速下载配置文 ...

  2. vue+springboot前后端分离实现单点登录跨域问题处理

    最近在做一个后台管理系统,前端是用时下火热的vue.js,后台是基于springboot的.因为后台系统没有登录功能,但是公司要求统一登录,登录认证统一使用.net项目组的认证系统.那就意味着做单点登 ...

  3. docker-compose 部署 Vue+SpringBoot 前后端分离项目

    一.前言 本文将通过docker-compose来部署前端Vue项目到Nginx中,和运行后端SpringBoot项目 服务器基本环境: CentOS7.3 Dokcer MySQL 二.docker ...

  4. 基于SpringBoot前后端分离的点餐系统

    基于SpringBoot前后端分离的点餐系统 开发环境:主要采用Spring boot框架和小程序开发 项目简介:点餐系统,分成卖家端和买家端.买家端使用微信小程序开发,实现扫码点餐.浏览菜单.下单. ...

  5. springboot 前后端分离开发 从零到整(三、登录以及登录状态的持续)

    今天来写一下怎么登录和维持登录状态. 相信登录验证大家都比较熟悉,在Javaweb中一般保持登录状态都会用session.但如果是前后端分离的话,session的作用就没有那么明显了.对于前后端分离的 ...

  6. springboot 前后端分离开发 从零到整(一、环境的搭建)

    第一次写文章,有什么错误地方请大家指正,也请大家见谅. 这次为大家分享我做毕业设计的一个过程,之前没有接触过springboot,一直做的都是Javaweb和前端,做了几个前后端分离的项目.现在听说s ...

  7. JEECG-Boot 项目介绍——基于代码生成器的快速开发平台(Springboot前后端分离)

    Jeecg-Boot 是一款基于代码生成器的智能开发平台!采用前后端分离架构:SpringBoot,Mybatis,Shiro,JWT,Vue&Ant Design.强大的代码生成器让前端和后 ...

  8. springboot 前后端分离开发 从零到整(二、邮箱注册)

    spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: ...

  9. SpringCloud SpringBoot 前后端分离企业级微服务架构源码赠送

    基于SpringBoot2.x.SpringCloud和SpringCloudAlibaba并采用前后端分离的企业级微服务敏捷开发系统架构.并引入组件化的思想实现高内聚低耦合,项目代码简洁注释丰富上手 ...

随机推荐

  1. [原创]Cadence Allegro16.6安装

    选择Cancel. lisence managner安装完成,然后安装Product installation Cadence OrCad Allegro SPB 16.6 完整版+和谐文件 链接:h ...

  2. Service Fabric是什么?

    题记:鉴于社区对Service Fabric有诸多误解,希望借本文能让大家正确了解Service Fabric是一个什么东西,算是给其正名. 术语与分类 Service Fabric不仅仅是容器编排器 ...

  3. Windows10远程报错:由于CredSSP加密Oracle修正导致远程失败

    解决方案:Windows 10 家庭版,没有 gpedit.msc,只能修改本地电脑的注册表,在本地“运行”输入: regedit   按以下目录进入:HKEY_LOCAL_MACHINE\Softw ...

  4. Spring常用注解总结

    转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...

  5. 问题之Spring MVC配置后,可以打开jsp页面,但打不开html页面

    一.配置Spring MVC 1.导入jar spring框架:http://repo.spring.io/release/org/springframework/spring/ spring-fra ...

  6. margin-top的兼容问题

    产生的条件:子元素给了margin-top,并且父元素没有浮也没有其他样式,浏览器解析的结果是父元素下去了. 解决方法:1.给子元素或者父元素添加浮动,缺点:如果不需要浮动,添加浮动也页面布局会乱 2 ...

  7. PHP常用日期加减计算方法实例

    PHP常用日期加减计算方法实例 实例总结了PHP常用日期加减计算方法.分享给大家供大家参考,具体如下: PHP 标准的日期格式 date("Y-m-d H:i:s"); PHP 简 ...

  8. idea在springboot项目中没有【新建JSP选项】的解决方法

    https://blog.csdn.net/qq_26525215/article/details/53726690 转载于CSDN

  9. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  10. 《SpringMVC从入门到放肆》十、SpringMVC注解式开发(复杂参数接收)

    上一篇我们学习了简单的参数接收方式,以及对编码的统一处理.今天我们来接收对象参数. 一.接收对象参数 jsp页面: <%@ page language="java" impo ...