溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用后端的Long型參數(shù)

發(fā)布時(shí)間:2021-10-28 16:25:41 來源:億速云 閱讀:303 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“如何使用后端的Long型參數(shù)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

一、問題描述

最近在改造內(nèi)部管理系統(tǒng)的時(shí)候, 發(fā)現(xiàn)了一個(gè)巨坑,就是前端 JS 在獲取后端 Long 型參數(shù)時(shí),出現(xiàn)精度丟失!

起初,用 postman 模擬接口請(qǐng)求,都很正常,但是用瀏覽器請(qǐng)求的時(shí)候,就出現(xiàn)問題了!

問題復(fù)現(xiàn):

@RequestMapping("/queryUser") public List<User> queryUser(){     List<User> resultList = new ArrayList<>();     User user = new User();     //賦予一個(gè)long型用戶ID     user.setId(123456789012345678L);     resultList.add(user);     return resultList; }

打開瀏覽器,請(qǐng)求接口,結(jié)果如下!

如何使用后端的Long型參數(shù)

用 postman 模擬接口請(qǐng)求,結(jié)果如下!

如何使用后端的Long型參數(shù)

剛開始的時(shí)候,還真沒發(fā)現(xiàn)這個(gè)坑,結(jié)果當(dāng)進(jìn)行測(cè)試的時(shí)候,才發(fā)現(xiàn)前端傳給后端的ID,與數(shù)據(jù)庫中存的ID不一致,才發(fā)現(xiàn) JavaScript  還有這個(gè)天坑!

由于 JavaScript 中 Number 類型的自身原因,并不能完全表示 Long 型的數(shù)字,在 Long  長(zhǎng)度大于17位時(shí)會(huì)出現(xiàn)精度丟失的問題。

當(dāng)我們把上面的用戶 ID 改成 19 位的時(shí)候,我們?cè)賮砜纯礊g覽器請(qǐng)求返回的結(jié)果。

//設(shè)置用戶ID,位數(shù)為19位 user.setId(1234567890123456789l);

瀏覽器請(qǐng)求結(jié)果!

如何使用后端的Long型參數(shù)

當(dāng)返回的結(jié)果超過17位的時(shí)候,后面的全部變成0!

二、解決辦法

遇到這種情況,應(yīng)該怎么辦呢?

  • 第一種辦法:在后臺(tái)把 long 型改為String類型,但是代價(jià)有點(diǎn)大,只要涉及到的地方都需要改

  • 第二種辦法:使用工具進(jìn)行轉(zhuǎn)化把 long 型改為String類型,這種方法可以實(shí)現(xiàn)全局轉(zhuǎn)化(推薦)

  • 第三種辦法:前端進(jìn)行處理(目前沒有很好的辦法,不推薦)

因?yàn)轫?xiàng)目涉及到的代碼非常多,所以不可能把 long 型改為 String 類型,而且使用 Long  類型的方法非常多,改起來風(fēng)險(xiǎn)非常大,所以不推薦使用!

最理想的方法,就是使用aop代理攔截所有的方法,對(duì)返回參數(shù)進(jìn)行統(tǒng)一處理,使用工具進(jìn)行轉(zhuǎn)化,過程如下!

1. Jackson 工具序列化對(duì)象

我們可以使用Jackson工具包來實(shí)現(xiàn)對(duì)象序列化。

首先在 maven 中添加必須的依賴:

<!--jackson依賴--> <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>

編寫一個(gè)轉(zhuǎn)化工具類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;         }     } }

緊接著,編寫一個(gè)實(shí)體類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; }

最后,我們編寫一個(gè)測(cè)試類測(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é)果如下:

如何使用后端的Long型參數(shù)

其中最關(guān)鍵一行代碼,是注冊(cè)了這個(gè)轉(zhuǎn)換類,從而實(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)化類寫好之后,就非常簡(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)類,繼承自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)目,操作也類似。

編寫一個(gè)WebConfig配置類,并實(shí)現(xiàn)自WebMvcConfigurer,重寫configureMessageConverters方法:

/**  * WebMvc配置  */ @Configuration @Slf4j @EnableWebMvc public class WebConfig implements WebMvcConfigurer {      /**      *添加消息轉(zhuǎn)化類      * @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);     } }

“如何使用后端的Long型參數(shù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI