码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Java 解决long类型数据在前后端传递失真问题


    问题:雪花算法的id长度为19位,前端能够接收的数字最多只能是16位的,因此就会造成精度丢失,得到的ID不是真正的ID。

    解决:

    在拦截器中加入Long类型转换,返回给前端string

    1. package io.global.iot.common.config;
    2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
    3. import com.fasterxml.jackson.annotation.PropertyAccessor;
    4. import com.fasterxml.jackson.databind.ObjectMapper;
    5. import com.fasterxml.jackson.databind.SerializationFeature;
    6. import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
    7. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
    8. import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
    9. import org.springframework.context.annotation.Configuration;
    10. import org.springframework.http.converter.HttpMessageConverter;
    11. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
    12. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    13. import java.time.LocalDateTime;
    14. import java.time.format.DateTimeFormatter;
    15. import java.util.List;
    16. @Configuration
    17. public class MvcConfiguration implements WebMvcConfigurer {
    18. @Override
    19. public void extendMessageConverters(List> converters) {
    20. MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    21. ObjectMapper objectMapper = new ObjectMapper();
    22. objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    23. objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    24. JavaTimeModule javaTimeModule = new JavaTimeModule();
    25. javaTimeModule.addSerializer(LocalDateTime.class,
    26. new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    27. javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance);
    28. javaTimeModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    29. objectMapper.registerModule(javaTimeModule);
    30. messageConverter.setObjectMapper(objectMapper);
    31. converters.add(0, messageConverter);
    32. }
    33. }

  • 相关阅读:
    Python数据攻略-SQL和Mongodb的CRUD方法汇总
    采用cv2和默认的人脸识别分类器实现人脸检测功能
    RDM1 软件需求与开发
    OREPA:阿里提出训练也很快的重参数策略,内存减半,速度加倍 | CVPR 2022
    三、基于图像分类预训练编码及图神经网络的预测模型 【框图+源码】
    基于C#实现的巧移火柴棍(火柴棍移动)
    Linux系统编程(五)——Linux下的多线程
    NLP Step by Step -- How to use pipeline
    搭建基于Django的博客系统增加广告轮播图(三)
    山形三元组
  • 原文地址:https://blog.csdn.net/qq_43560721/article/details/132912672
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号