溫馨提示×

溫馨提示×

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

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

javascript接收long類型參數(shù)時精度丟失怎么處理

發(fā)布時間:2022-03-01 13:46:43 來源:億速云 閱讀:1688 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了javascript接收long類型參數(shù)時精度丟失怎么處理的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇javascript接收long類型參數(shù)時精度丟失怎么處理文章都會有所收獲,下面我們一起來看看吧。

一、問題描述

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

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

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

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

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

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

當我們把上面的用戶 ID 改成 19 位的時候,我們再來看看瀏覽器請求返回的結(jié)果。

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

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

二、解決辦法

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

  • 第一種辦法:在后臺把 long型改為String類型,但是代價有點大,只要涉及到的地方都需要改

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

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

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

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

2.1、Jackson 工具序列化對象

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

  • 首先在 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>
  • 編寫一個轉(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 {
        // 對象的所有字段全部列入
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        // 取消默認轉(zhuǎn)換timestamps形式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        // 忽略空bean轉(zhuǎn)json的錯誤
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        //設(shè)置為東八區(qū)
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        // 統(tǒng)一日期格式
        objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT));
        // 反序列化時,忽略在json字符串中存在, 但在java對象中不存在對應(yīng)屬性的情況, 防止錯誤
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 序列換成json時,將所有的long變成string
        objectMapper.registerModule(new SimpleModule().addSerializer(Long.class, ToStringSerializer.instance).addSerializer(Long.TYPE, ToStringSerializer.instance));
    }


    /**
     * 對象序列化成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字符串反序列化成對象
     * @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字符串反序列化成對象(數(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;
        }
    }
}
  • 緊接著,編寫一個實體類Person,用于測試

@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;
}
  • 最后,我們編寫一個測試類測試一下效果

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é)果如下:

javascript接收long類型參數(shù)時精度丟失怎么處理

其中最關(guān)鍵一行代碼,是注冊了這個轉(zhuǎn)換類,從而實現(xiàn)將所有的 long 變成 string

// 序列換成json時,將所有的long變成string
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);

如果想對某個日期進行格式化,可以全局設(shè)置。

//全局統(tǒng)一日期格式
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

也可以,單獨對某個屬性進行設(shè)置,例如對createTime屬性格式化為yyyy-MM-dd,只需要加上如下注解即可。

@JsonFormat(pattern="yyyy-MM-dd", timezone="GMT+8")
private Date createTime;

工具轉(zhuǎn)化類寫好之后,就非常簡單了,只需要對 aop 攔截的方法返回的參數(shù),進行序列化就可以自動實現(xiàn)將所有的 long變成 string。

2.2、SpringMVC 配置

如果是 SpringMVC 項目,操作也很簡單。

  • 自定義一個實現(xiàn)類,繼承自ObjectMapper

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">
                        <-對日期進行統(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>

2.3、SpringBoot 配置

如果是 SpringBoot 項目,操作也類似。

  • 編寫一個WebConfig配置類,并實現(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時,將所有的long變成string
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        list.add(jsonConverter);
    }
}

關(guān)于“javascript接收long類型參數(shù)時精度丟失怎么處理”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“javascript接收long類型參數(shù)時精度丟失怎么處理”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

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

AI