在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. elasticsearch 学习

    docker run -p : -d elasticsearch #直接拉取运行 #指定条件搜索curl --request GET \ --url 'http://localhost:9200/im ...

  2. C# dynamic类型序列化和反序列化之Newtonsoft.Json,动态解析远端返回的jSON数据

    一.说明 1.Newtonsoft.Json 中的Linq To Json中提供了方便的json数据查询.修改等操作. 例如:JObject,JArray 2.在JObject.FromObject( ...

  3. Redis 常用命令总结

    连接操作相关的命令 quit:关闭连接(connection) auth:简单密码认证 持久化 save:将数据同步保存到磁盘 bgsave:将数据异步保存到磁盘 lastsave:返回上次成功将数据 ...

  4. SQL反模式学习笔记1 开篇

    什么是“反模式” 反模式是一种试图解决问题的方法,但通常会同时引发别的问题. 反模式分类 (1)逻辑数据库设计反模式 在开始编码之前,需要决定数据库中存储什么信息以及最佳的数据组织方式和内在关联方式. ...

  5. ubuntu16.04之mongodb自动备份

    cron服务是Linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: service cron start service cron stop service cron resta ...

  6. web.xml中Servlet3.1版本的头信息格式

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  7. 安装es6编译babel

    1.它的安装命令如下. 全局安装 :$ npm install --global babel-cli项目下安装: $ npm install -g babel-cli --save-dev 2.配置. ...

  8. Python-模块,以及使用文本中的数据

    模块导入: from math import pi as math_pi print math_pi     #相当于把pi取了个别名 # -*- coding: cp936 -*-from rand ...

  9. centos7 安装 oh my zsh

    和在ubuntu 下安装十分相似(基本没区别) 安装zsh yum install zsh 改变系统bash chsh -s /bin/zsh git clone oh my zsh 项目: git@ ...

  10. webpack 4+ vue-loader 配置 (完善中...)

    webpack 4+ vue-loader 配置 写的demo,clone下来后,npm run dev即可,(此demo并未加入router) 可能会由于版本问题,配置会有些许改动,暂时都是可用的 ...