精度丢失问题

简介: BFF Client 使用的 npm 包 request-promise-native 请求微服务接口返回 ID 精度丢失

6.png


背景


  • BFF Client 使用的 npm 包 request-promise-native 请求微服务接口返回 ID 精度丢失

1713166949059674112 => 1713166949059674000


为什么会丢失?


  • 存储二进制时小数点的偏移量最大为52位,计算机存储的为二进制,而能存储的二进制为62位,超出就会有舍入操作,因此 JS 中能精准表示的最大整数是 Math.pow(2, 53),十进制即9007199254740992大于 9007199254740992 的可能会丢失精度



  • request-promise-native 发起请求时,当options.json 不为 false 会使用 JSON.parse 解析 body


if (self._json) {
  try {
    response.body = JSON.parse(response.body, self._jsonReviver)
  } catch (e) {
    debug('invalid JSON received', self.uri.href)
  }
}


最小 demo


搭建服务 API


一、搭建 Java Web Api:



public long getId() {
        return id + 1713166949059674112L;
    }
* 修改 controller 层添加 post 请求
    @PostMapping("/greeting_create")
    public Greeting createGreeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }


二、请求


  • GET 请求: curl http://localhost:8080/greeting
  • POST 请求:curl -X POST http://localhost:8080/greeting_create


{"id":1713166949059674120,"content":"Hello, World!"}


解决方案


1. 获取响应体的字符串,使用 JSONbigid 转化成字符串


  • 优点:只影响当前请求
  • 缺点:不支持 POST 请求方式,


  • 通过 json 传参数不支持
  • 通过 form + json: false 传参数需要后端接口支持


  • GET 请求


const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
  const getOptions = {
    'method': 'GET',
    json: false,
    'url': 'http://localhost:8080/greeting',
  };
  const getRes = await rp(getOptions);
  console.log('get result: ', jsonBigInt.parse(getRes));


  • POST 请求:不支持,json 被占用,一定会执行 JSON.parse


const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
  const postOptions = {
    'method': 'POST',
    'url': 'http://localhost:8080/greeting_create',
    json: { name: 'test' },
  };
  const postRes = await rp(postOptions);
  console.log('post result: ', jsonBigInt.parse(postRes));


2. 使用 JSONbig.parse() 替换 JSON.parse()


  • 优点:实现简单,支持 POST
  • 缺点:影响所有的 JSON.parse() 解析


const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
async function jsonBigReplaceParse() {
  const oldParse = JSON.parse;
  JSON.parse = jsonBigInt.parse;
  const postOptions = {
    'method': 'POST',
    'url': 'http://localhost:8080/greeting_create',
    json: { name: 'test' },
  };
  const postRes = await rp(postOptions);
  console.log('post result: ', postRes);
  JSON.parse = oldParse;
}


~ 本文完,感谢阅读!


学习有趣的知识,结识有趣的朋友,塑造有趣的灵魂!




相关文章
|
存储 人工智能 自然语言处理
Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索、常用配置参数、聚合功能等详解]
Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索、常用配置参数、聚合功能等详解]
Elasticsearch Relevance Engine---为AI变革提供高级搜索能力[ES向量搜索、常用配置参数、聚合功能等详解]
|
2月前
|
人工智能 自然语言处理 算法
【2025云栖大会】AI 搜索智能探索:揭秘如何让搜索“有大脑”
2025云栖大会上,阿里云高级技术专家徐光伟在云栖大会揭秘 Agentic Search 技术,涵盖低维向量模型、多模态检索、NL2SQL及DeepSearch/Research智能体系统。未来,“AI搜索已从‘信息匹配’迈向‘智能决策’,阿里云将持续通过技术创新与产品化能力,为企业构建下一代智能信息获取系统。”
415 9
|
3月前
|
人工智能 Java API
Java与大模型集成实战:构建智能Java应用的新范式
随着大型语言模型(LLM)的API化,将其强大的自然语言处理能力集成到现有Java应用中已成为提升应用智能水平的关键路径。本文旨在为Java开发者提供一份实用的集成指南。我们将深入探讨如何使用Spring Boot 3框架,通过HTTP客户端与OpenAI GPT(或兼容API)进行高效、安全的交互。内容涵盖项目依赖配置、异步非阻塞的API调用、请求与响应的结构化处理、异常管理以及一些面向生产环境的最佳实践,并附带完整的代码示例,助您快速将AI能力融入Java生态。
581 12
|
JSON JavaScript 前端开发
JSON.parse()和JSON.stringify()用法
JSON.parse()和JSON.stringify()用法
717 1
|
数据管理 Linux Shell
export命令详解
export命令详解
|
域名解析 安全 Java
SpringBoot启动的时候初始化的线程池默认配置tomcat
SpringBoot启动的时候初始化的线程池默认配置tomcat
648 1
|
开发工具 git 开发者
合理使用WebStorm-好用的Git工具(上)
合理使用WebStorm-好用的Git工具(上)
合理使用WebStorm-好用的Git工具(上)
|
数据采集 机器学习/深度学习 人工智能
数据标注(二)
数据标注(二)
1286 0