最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot jackson 精度處理問題解決

 更新時間:2024年10月11日 10:10:36   作者:酒醉的胡鐵  
由于JavaScript處理的最大數(shù)值限制,較大的雪花ID在JS中容易溢出,為解決此問題,可在SpringMVC或SpringBoot中使用@RequestBody注解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

問題說明

因為js能處理的最大值和最小值分別是

private static final long MAX_SAFE_INTEGER = 9007199254740991L;
private static final long MIN_SAFE_INTEGER = -9007199254740991L;

所以我們的雪花id很容易就超出了這個范圍,所以要轉(zhuǎn)換為字符串做適配
例如:1692419165819899402 就會變成1692419165819800000

解決方案

入?yún)?/h3>

在Spring MVC或Spring Boot中,可以使用@RequestBody將字符串類型的JSON字段映射到Java中的long類型字段,只要字符串內(nèi)容是一個有效的long值。Spring的Jackson庫會自動處理這個轉(zhuǎn)換。需要注意處理可能的轉(zhuǎn)換異常,以確保應(yīng)用的健壯性。

返回需要配置處理器

實體類字段就可以定義為Long類型了

package com.platform.paycenter.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.platform.paycenter.handler.BigNumberSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * jackson 配置
 *
 * @author Lion Li
 */
@Slf4j
@Configuration
public class JacksonConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        return builder -> {
            // 全局配置序列化返回 JSON 處理
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
            javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
            javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
            javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
            javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
            builder.modules(javaTimeModule);
            builder.timeZone(TimeZone.getDefault());
            log.info("初始化 jackson 配置");
        };
    }

}

package com.platform.paycenter.handler;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;

import java.io.IOException;

/**
 * 超出 JS 最大最小值 處理
 *
 * @author Lion Li
 */
@JacksonStdImpl
public class BigNumberSerializer extends NumberSerializer {

    /**
     * 根據(jù) JS Number.MAX_SAFE_INTEGER 與 Number.MIN_SAFE_INTEGER 得來
     */
    private static final long MAX_SAFE_INTEGER = 9007199254740991L;
    private static final long MIN_SAFE_INTEGER = -9007199254740991L;

    /**
     * 提供實例
     */
    public static final BigNumberSerializer INSTANCE = new BigNumberSerializer(Number.class);

    public BigNumberSerializer(Class<? extends Number> rawType) {
        super(rawType);
    }

    @Override
    public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        // 超出范圍 序列化位字符串
        if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) {
            super.serialize(value, gen, provider);
        } else {
            gen.writeString(value.toString());
        }
    }
}

到此這篇關(guān)于SpringBoot jackson 精度處理問題解決的文章就介紹到這了,更多相關(guān)SpringBoot jackson 精度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中字符串常見題之String相關(guān)講解

    Java中字符串常見題之String相關(guān)講解

    今天小編就為大家分享一篇關(guān)于Java中字符串常見題之String相關(guān)講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • IDEA 集成log4j將SQL語句打印在控制臺上的實現(xiàn)操作

    IDEA 集成log4j將SQL語句打印在控制臺上的實現(xiàn)操作

    這篇文章主要介紹了IDEA 集成log4j將SQL語句打印在控制臺上的實現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java反射使用的詳細(xì)介紹(最新推薦)

    Java反射使用的詳細(xì)介紹(最新推薦)

    這篇文章主要介紹了Java反射使用的詳細(xì)介紹,反射的第一步都是先得到編譯后的Class類對象,然后就可以得到Class的全部成分,本文結(jié)合實例代碼詳細(xì)講解,需要的朋友可以參考下
    2023-02-02
  • Spring實戰(zhàn)之清除緩存操作示例

    Spring實戰(zhàn)之清除緩存操作示例

    這篇文章主要介紹了Spring實戰(zhàn)之清除緩存操作,結(jié)合實例形式詳細(xì)分析了spring清除緩存操作具體步驟、配置、領(lǐng)域模型及相關(guān)使用技巧,需要的朋友可以參考下
    2020-01-01
  • Java之while與do-while循環(huán)的用法詳解

    Java之while與do-while循環(huán)的用法詳解

    在上一篇文章中,給大家講解了循環(huán)的概念,并重點給大家講解了for循環(huán)的使用。但在Java中,除了for循環(huán)之外,還有while、do-while、foreach等循環(huán)形式。這篇文章給大家講解while循環(huán)的使用
    2023-05-05
  • mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法

    mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法

    本文主要介紹了mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Springboot工具類StringUtils使用教程

    Springboot工具類StringUtils使用教程

    這篇文章主要介紹了Springboot內(nèi)置的工具類之StringUtils的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12
  • jvm調(diào)優(yōu)常用命令行工具詳解

    jvm調(diào)優(yōu)常用命令行工具詳解

    這篇文章主要介紹了jvm調(diào)優(yōu)常用命令行工具的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • SpringBoot實現(xiàn)微服務(wù)通信的多種方式

    SpringBoot實現(xiàn)微服務(wù)通信的多種方式

    微服務(wù)通信是指在分布式系統(tǒng)中,各個微服務(wù)之間進(jìn)行數(shù)據(jù)交互和通信的過程,今天我們將探討在Spring Boot中實現(xiàn)微服務(wù)通信的多種方式,文章通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • java計算自冪數(shù)和水仙花數(shù)

    java計算自冪數(shù)和水仙花數(shù)

    對于一個正整數(shù)而言,長度是n,如果它的各位上的數(shù)字的n次方之和正好等于它本身,那么我們稱這樣的數(shù)為自冪數(shù),下面使用JAVA實現(xiàn)這個方法
    2014-03-03

最新評論

章丘市| 石泉县| 饶平县| 奉节县| 卢氏县| 武川县| 平舆县| 福海县| 铁岭县| 北宁市| 肇源县| 如皋市| 建阳市| 腾冲县| 三台县| 兴宁市| 小金县| 紫金县| 义马市| 兰溪市| 伊川县| 佛山市| 韶山市| 勃利县| 通河县| 台东市| 芷江| 高雄市| 出国| 应用必备| 淄博市| 宁海县| 双桥区| 龙川县| 灵石县| 阿克陶县| 大理市| 永仁县| 沂源县| 博湖县| 都昌县|