后端的Long型參數(shù),讓阿粉踩了個(gè)大坑
最近幾天一直在改造工程,采用雪花算法生成主鍵ID,突然踩到一個(gè)天坑,前端 JavaScript 在取 Long 型參數(shù)時(shí),參數(shù)值有點(diǎn)不太對(duì)!
一、問(wèn)題描述
最近在改造內(nèi)部管理系統(tǒng)的時(shí)候, 發(fā)現(xiàn)了一個(gè)巨坑,就是前端 JS 在獲取后端 Long 型參數(shù)時(shí),出現(xiàn)精度丟失!
起初,用 postman 模擬接口請(qǐng)求,都很正常,但是用瀏覽器請(qǐng)求的時(shí)候,就出現(xiàn)問(wèn)題了!
問(wèn)題復(fù)現(xiàn):
- @RequestMapping("/queryUser")
- public List<User> queryUser(){
- List<User> resultList = new ArrayList<>();
- User user = new User();
- //賦予一個(gè)long型用戶(hù)ID
- user.setId(123456789012345678L);
- resultList.add(user);
- return resultList;
- }
打開(kāi)瀏覽器,請(qǐng)求接口,結(jié)果如下!
用 postman 模擬接口請(qǐng)求,結(jié)果如下!
剛開(kāi)始的時(shí)候,還真沒(méi)發(fā)現(xiàn)這個(gè)坑,結(jié)果當(dāng)進(jìn)行測(cè)試的時(shí)候,才發(fā)現(xiàn)前端傳給后端的ID,與數(shù)據(jù)庫(kù)中存的ID不一致,才發(fā)現(xiàn) JavaScript 還有這個(gè)天坑!
由于 JavaScript 中 Number 類(lèi)型的自身原因,并不能完全表示 Long 型的數(shù)字,在 Long 長(zhǎng)度大于17位時(shí)會(huì)出現(xiàn)精度丟失的問(wèn)題。
當(dāng)我們把上面的用戶(hù) ID 改成 19 位的時(shí)候,我們?cè)賮?lái)看看瀏覽器請(qǐng)求返回的結(jié)果。
- //設(shè)置用戶(hù)ID,位數(shù)為19位
- user.setId(1234567890123456789l);
瀏覽器請(qǐng)求結(jié)果!
當(dāng)返回的結(jié)果超過(guò)17位的時(shí)候,后面的全部變成0!
二、解決辦法
遇到這種情況,應(yīng)該怎么辦呢?
- 第一種辦法:在后臺(tái)把 long 型改為String類(lèi)型,但是代價(jià)有點(diǎn)大,只要涉及到的地方都需要改
- 第二種辦法:使用工具進(jìn)行轉(zhuǎn)化把 long 型改為String類(lèi)型,這種方法可以實(shí)現(xiàn)全局轉(zhuǎn)化(推薦)
- 第三種辦法:前端進(jìn)行處理(目前沒(méi)有很好的辦法,不推薦)
因?yàn)轫?xiàng)目涉及到的代碼非常多,所以不可能把 long 型改為 String 類(lèi)型,而且使用 Long 類(lèi)型的方法非常多,改起來(lái)風(fēng)險(xiǎn)非常大,所以不推薦使用!
最理想的方法,就是使用aop代理攔截所有的方法,對(duì)返回參數(shù)進(jìn)行統(tǒng)一處理,使用工具進(jìn)行轉(zhuǎn)化,過(guò)程如下!
1. Jackson 工具序列化對(duì)象
我們可以使用Jackson工具包來(lái)實(shí)現(xiàn)對(duì)象序列化。
首先在 maven 中添加必須的依賴(lài):
- <!--jackson依賴(lài)-->
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <version>2.9.8</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-annotations</artifactId>
- <version>2.9.8</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.9.8</version>
- </dependency>
編寫(xiě)一個(gè)轉(zhuǎn)化工具類(lèi)JsonUtil:
- public class JsonUtil {
- private static final Logger log = LoggerFactory.getLogger(JsonUtil.class);
- private static ObjectMapper objectMapper = new ObjectMapper();
- private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
- static {
- // 對(duì)象的所有字段全部列入
- objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
- // 取消默認(rèn)轉(zhuǎn)換timestamps形式
- objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
- // 忽略空bean轉(zhuǎn)json的錯(cuò)誤
- objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
- //設(shè)置為東八區(qū)
- objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
- // 統(tǒng)一日期格式
- objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT));
- // 反序列化時(shí),忽略在json字符串中存在, 但在java對(duì)象中不存在對(duì)應(yīng)屬性的情況, 防止錯(cuò)誤
- objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- // 序列換成json時(shí),將所有的long變成string
- objectMapper.registerModule(new SimpleModule().addSerializer(Long.class, ToStringSerializer.instance).addSerializer(Long.TYPE, ToStringSerializer.instance));
- }
- /**
- * 對(duì)象序列化成json字符串
- * @param obj
- * @param <T>
- * @return
- */
- public static <T> String objToStr(T obj) {
- if (null == obj) {
- return null;
- }
- try {
- return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
- } catch (Exception e) {
- log.warn("objToStr error: ", e);
- return null;
- }
- }
- /**
- * json字符串反序列化成對(duì)象
- * @param str
- * @param clazz
- * @param <T>
- * @return
- */
- public static <T> T strToObj(String str, Class<T> clazz) {
- if (StringUtils.isBlank(str) || null == clazz) {
- return null;
- }
- try {
- return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
- } catch (Exception e) {
- log.warn("strToObj error: ", e);
- return null;
- }
- }
- /**
- * json字符串反序列化成對(duì)象(數(shù)組)
- * @param str
- * @param typeReference
- * @param <T>
- * @return
- */
- public static <T> T strToObj(String str, TypeReference<T> typeReference) {
- if (StringUtils.isBlank(str) || null == typeReference) {
- return null;
- }
- try {
- return (T) (typeReference.getType().equals(String.class) ? str : objectMapper.readValue(str, typeReference));
- } catch (Exception e) {
- log.warn("strToObj error", e);
- return null;
- }
- }
- }
緊接著,編寫(xiě)一個(gè)實(shí)體類(lèi)Person,用于測(cè)試:
- @Data
- public class Person implements Serializable {
- private static final long serialVersionUID = 1L;
- private Integer id;
- //Long型參數(shù)
- private Long uid;
- private String name;
- private String address;
- private String mobile;
- private Date createTime;
- }
最后,我們編寫(xiě)一個(gè)測(cè)試類(lèi)測(cè)試一下效果:
- public static void main(String[] args) {
- Person person = new Person();
- person.setId(1);
- person.setUid(1111L);
- person.setName("hello");
- person.setAddress("");
- System.out.println(JsonUtil.objToStr(person));
- }
輸出結(jié)果如下:
其中最關(guān)鍵一行代碼,是注冊(cè)了這個(gè)轉(zhuǎn)換類(lèi),從而實(shí)現(xiàn)將所有的 long 變成 string。
- // 序列換成json時(shí),將所有的long變成string
- SimpleModule simpleModule = new SimpleModule();
- simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
- simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
- objectMapper.registerModule(simpleModule);
如果想對(duì)某個(gè)日期進(jìn)行格式化,可以全局設(shè)置。
- //全局統(tǒng)一日期格式
- objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
也可以,單獨(dú)對(duì)某個(gè)屬性進(jìn)行設(shè)置,例如對(duì)createTime屬性格式化為yyyy-MM-dd,只需要加上如下注解即可。
- @JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
- private Date createTime;
工具轉(zhuǎn)化類(lèi)寫(xiě)好之后,就非常簡(jiǎn)單了,只需要對(duì) aop 攔截的方法返回的參數(shù),進(jìn)行序列化就可以自動(dòng)實(shí)現(xiàn)將所有的 long 變成 string。
2. SpringMVC 配置
如果是 SpringMVC 項(xiàng)目,操作也很簡(jiǎn)單。
自定義一個(gè)實(shí)現(xiàn)類(lèi),繼承自O(shè)bjectMapper:
- package com.example.util;
- /**
- * 繼承ObjectMapper
- */
- public class CustomObjectMapper extends ObjectMapper {
- public CustomObjectMapper() {
- super();
- SimpleModule simpleModule = new SimpleModule();
- simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
- simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
- registerModule(simpleModule);
- }
- }
在 SpringMVC 的配置文件中加上如下配置:
- <mvc:annotation-driven >
- <mvc:message-converters>
- <bean class="org.springframework.http.converter.StringHttpMessageConverter">
- <constructor-arg index="0" value="utf-8" />
- <property name="supportedMediaTypes">
- <list>
- <value>application/json;charset=UTF-8</value>
- <value>text/plain;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="objectMapper">
- <bean class="com.example.util.CustomObjectMapper">
- <property name="dateFormat">
- <-對(duì)日期進(jìn)行統(tǒng)一轉(zhuǎn)化->
- <bean class="java.text.SimpleDateFormat">
- <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
- </bean>
- </property>
- </bean>
- </property>
- </bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
3. SpringBoot 配置
如果是 SpringBoot 項(xiàng)目,操作也類(lèi)似。
編寫(xiě)一個(gè)WebConfig配置類(lèi),并實(shí)現(xiàn)自WebMvcConfigurer,重寫(xiě)configureMessageConverters方法:
- /**
- * WebMvc配置
- */
- @Configuration
- @Slf4j
- @EnableWebMvc
- public class WebConfig implements WebMvcConfigurer {
- /**
- *添加消息轉(zhuǎn)化類(lèi)
- * @param list
- */
- @Override
- public void configureMessageConverters(List<HttpMessageConverter<?>> list) {
- MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
- ObjectMapper objectMapper = jsonConverter.getObjectMapper();
- //序列換成json時(shí),將所有的long變成string
- SimpleModule simpleModule = new SimpleModule();
- simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
- simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
- objectMapper.registerModule(simpleModule);
- list.add(jsonConverter);
- }
- }
三、總結(jié)
在實(shí)際的項(xiàng)目開(kāi)發(fā)中,很多服務(wù)都是純微服務(wù)開(kāi)發(fā),沒(méi)有用到SpringMVC,在這種情況下,使用JsonUtil工具類(lèi)實(shí)現(xiàn)對(duì)象序列化,可能是一個(gè)非常好的選擇。