前端处理后端传来的Long型数据精度丢失的问题--对象转换器Jackson0bjectMapper

news/2024/10/18 16:52:51/

前端处理后端的Long型数据精度丢失的问题

1、问题描述

  1. 前端提交参数
{id: "1681224986113204200", status: 0}
  1. 后端处理
  /*** 根据员工id修改员工账号** @param employee* @return*/@PutMapping()public R<String> status(HttpServletRequest request, @RequestBody Employee employee) {log.info("员工状态的设置参数:{}", employee);employee.setUpdateTime(LocalDateTime.now());Long empId = (Long) request.getSession().getAttribute("EmployeeId");employee.setUpdateUser(empId);employeeService.updateById(employee);return R.success("员工状态修改成功");}

前端js对long型数据进行处理时丢失精度(只能保证16位),导致提交的id和数据库中的id不一致。

2、 问题实现

如何解决这个问题?

我们可以在服务端给页面响应json数据时进行处理,将long型数据统一转为String字符串

3、具体代码实现–对象转换器Jackson0bjectMapper

具体实现步骤:

  1. 提供对象转换器Jackson0bjectMapper,基于Jackson进行Java对象到json数据的转换(直接复制下面代码到项目中使用)
package com.mannor.reggie_take_out.common;import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;/*** 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]*/
public class JacksonObjectMapper extends ObjectMapper {public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";public JacksonObjectMapper() {super();//收到未知属性时不报异常this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);//反序列化时,属性不存在的兼容处理this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);SimpleModule simpleModule = new SimpleModule().addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))).addSerializer(BigInteger.class, ToStringSerializer.instance).addSerializer(Long.class, ToStringSerializer.instance).addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));//注册功能模块 例如,可以添加自定义序列化器和反序列化器this.registerModule(simpleModule);}
}

此工具不仅可以对数据进行json处理,还对时间进行了格式化处理,让前端处理更方便。

  1. 在WebMvcConfig配置类中扩展Spring mvc的消息转换器,在此消息转换器中使用提供的对象转换器进行Java对象到json数据的转换
package com.mannor.reggie_take_out.config;import com.mannor.reggie_take_out.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.util.List;@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {/*** 扩展MVC框架的消息转换器* @param converters*/@Overrideprotected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {log.info("扩展到额消息转换器.....");//创建消息转换器对象MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();//设置转换器对象,底层使用jackson对象将java对象转为jsonmessageConverter.setObjectMapper(new JacksonObjectMapper());//将上面的消息转换器对象追加带mvc框架的转换器集合中converters.add(0,messageConverter);//为0是设置优先使用我们定义的此消息转换器super.extendMessageConverters(converters);}
}

自此,转换就已经生效了,重启服务即可测试。


http://www.ppmy.cn/news/950442.html

相关文章

【JavaScript 03】语句 变量 变量提升 标识符 注释 区块 作用域 条件语句 循环语句 break continue 标签

CSS 语句变量变量提升标识符注释区块条件语句ifif...elseswitch三元运算符 ?: 循环语句whilefordo...whilebreak语句 and continue语句标签 语句 JS程序的执行单位是行&#xff08;line&#xff09; **语句&#xff08;statement&#xff09;**是为了完成特定任务而进行的操…

PMP证书怎么考?来自前辈的经验之谈

虽然我成功通过了 PMP 考试&#xff0c;这是我第一季度所做的事情的成果&#xff0c;但是考试的兴奋感很快就过去了。在交流群里&#xff0c;大家都在讨论证书的存续条件以及获得证书后带来的实质性收益&#xff0c;例如补贴、城市定居加分和职业晋升等。与这些相比&#xff0c…

InstructGPT:chatGPT的姊妹模型

1.简介 本文根据2022年《Training language models to follow instructions with human feedback》翻译总结的&#xff0c;文中提出了InstructGPT&#xff0c;从文章标题也可以看出来训练模型采用了人类指导反馈的数据。 因为chatGPT说是InstructGPT的姊妹模型&#xff0c;所…

【ChatGPT】ChatGPT的核心算法原理图文解析、大模型训练过程和数据集来源

目录 ChatGPT的核心算法、模型和数据来源 一、引言:ChatGPT的概述与背景

ChatGPT的模型介绍及GO语言实现API

ChatGPT除了大家熟悉的GPT3之外&#xff0c;还有其他辅助模型&#xff0c;比如处理代码的以及有害信息过滤的系统。总的来说是下面三个组成&#xff1a; GPT-3&#xff1a;一组能够理解和生成自然语言的模型 CodexLimited beta&#xff1a;一组可以理解和生成代码的模型&#x…

超越ChatGPT:大模型的智能极限

在此前《大型语言模型的涌现能力》、《ChatGPT进化的秘密》两篇文章中&#xff0c;符尧剖析了大型语言模型的突现能力和潜在优势&#xff0c;大模型所带来的“潜在的”范式转变&#xff0c;并拆解了ChatGPT演进的技术路线图。 在本文中&#xff0c;作者以终为始分析了大模型的智…

ChatGPT基础知识系列之模型介绍

ChatGPT基础知识系列之模型介绍 前面我们已经介绍很多ChatGPT的使用案例了,更多案例可以参考我们下面的文章 ChatGPT使用案例之写代码 ChatGPT使用案例之画思维导图 ChatGPT使用案例之自然语言处理 ChatGPT使用案例之操作Excel ChatGPT使用案例之图像生成 ChatGPT使用案…

ChatGPT概述:从模型训练到基本应用的介绍

ChatGPT概述&#xff1a;从模型训练到基本应用的介绍 目录 本文是对ChatGPT的由来、训练过程以及实际落地场景的解释&#xff0c;主要内容包括如下三个方面&#xff1a; 1、ChatGPT是什么 2、ChatGPT的原理 3、ChatGPT的思考 4、ChatGPT的应用 ChatGPT是什么 ChatGPT可能是近…