From 7e613f8da611c984b6638015d9e0a697f1cfde48 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 29 七月 2021 13:11:35 +0800 Subject: [PATCH] add 增加 jackson 超出 JS 最大数值自动转字符串(雪花id序列化)处理 --- ruoyi-framework/src/main/java/com/ruoyi/framework/config/JacksonConfig.java | 8 ++++++++ ruoyi-framework/src/main/java/com/ruoyi/framework/jackson/BigNumberSerializer.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 0 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/JacksonConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/JacksonConfig.java index 8334b2a..4346610 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/JacksonConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/JacksonConfig.java @@ -2,15 +2,19 @@ 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.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.ruoyi.common.utils.JsonUtils; +import com.ruoyi.framework.jackson.BigNumberSerializer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; 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.util.TimeZone; @@ -34,6 +38,10 @@ ObjectMapper objectMapper = (ObjectMapper) bean; // 鍏ㄥ眬閰嶇疆搴忓垪鍖栬繑鍥� JSON 澶勭悊 SimpleModule simpleModule = new SimpleModule(); + simpleModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE); + simpleModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE); + simpleModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE); + simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance); simpleModule.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE); simpleModule.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); objectMapper.registerModule(simpleModule); diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/jackson/BigNumberSerializer.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/jackson/BigNumberSerializer.java new file mode 100644 index 0000000..792da8b --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/jackson/BigNumberSerializer.java @@ -0,0 +1,29 @@ +package com.ruoyi.framework.jackson; + +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; + +@JacksonStdImpl +public class BigNumberSerializer extends NumberSerializer { + + private static final long JS_NUM_MAX = 9007199254740992L; + private static final long JS_NUM_MIN = -9007199254740992L; + 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() >= JS_NUM_MIN && value.longValue() <= JS_NUM_MAX) { + super.serialize(value, gen, provider); + } else { + gen.writeString(value.toString()); + } + } +} -- Gitblit v1.9.3