优化:基于阿里巴巴 Java 开发手册(黄山版)优化 Jackson 超大整数配置

1.编程规约>前后端规约>第6条:
【强制】对于需要使用超大整数的场景,服务端一律使用 String 字符串类型返回,禁止使用 Long 类型。
说明:Java 服务端如果直接返回 Long 整型数据给前端,Javascript 会自动转换为 Number 类型(注:此类型为双精度浮点数,表示原理与取值范围等同于 Java 中的 Double)。Long 类型能表示的最大值是 263-1,在取值范围之内,超过 253(9007199254740992)的数值转化为Javascript 的 Number 时,有些数值会产生精度损失。
扩展说明,在 Long 取值范围内,任何 2 的指数次的整数都是绝对不会存在精度损失的,所以说精度损失是一个概率问题。若浮点数尾数位与指数位空间不限,则可以精确表示任何整数,但很不幸,双精度浮点数的尾数位只有 52 位。
反例:通常在订单号或交易号大于等于 16 位,大概率会出现前后端订单数据不一致的情况。比如,后端传输的 "orderId":362909601374617692,前端拿到的值却是:362909601374617660
This commit is contained in:
2023-03-05 19:31:02 +08:00
parent 95784e5c7d
commit 8823211fd9
14 changed files with 57 additions and 112 deletions

View File

@@ -1,55 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.config.jackson;
import java.io.IOException;
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;
/**
* 大数值序列化器(针对数值超出 JS 最大或最小值的情况,将其转换为字符串)
*
* @author Charles7c
* @since 2022/12/11 13:22
*/
@JacksonStdImpl
public class BigNumberSerializer extends NumberSerializer {
/** 静态实例 */
public static final BigNumberSerializer SERIALIZER_INSTANCE = new BigNumberSerializer(Number.class);
/** JSNumber.MAX_SAFE_INTEGER */
private static final long MAX_SAFE_INTEGER = 9007199254740991L;
/** JSNumber.MIN_SAFE_INTEGER */
private static final long MIN_SAFE_INTEGER = -9007199254740991L;
public BigNumberSerializer(Class<? extends Number> rawType) {
super(rawType);
}
@Override
public void serialize(Number value, JsonGenerator generator, SerializerProvider provider) throws IOException {
// 序列化为字符串
if (value.longValue() > MIN_SAFE_INTEGER && value.longValue() < MAX_SAFE_INTEGER) {
super.serialize(value, generator, provider);
} else {
generator.writeString(value.toString());
}
}
}

View File

@@ -61,11 +61,11 @@ public class JacksonConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
// 针对数值类型Long、BigInteger、BigDecimal 的序列化和反序列化
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Long.class, BigNumberSerializer.SERIALIZER_INSTANCE);
javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.SERIALIZER_INSTANCE);
javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.SERIALIZER_INSTANCE);
// 针对数值类型Long、BigInteger、BigDecimal 的序列化
javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance);
javaTimeModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
javaTimeModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
// 针对时间类型LocalDateTime、LocalDate、LocalTime 的序列化和反序列化
@@ -80,9 +80,9 @@ public class JacksonConfiguration {
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(StringConsts.NORM_TIME_PATTERN);
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));
builder.modules(javaTimeModule);
builder.timeZone(TimeZone.getDefault());
log.info(">>>初始化 Jackson 配置<<<");
builder.modules(javaTimeModule);
log.info(">>>初始化 Jackson 序列化配置<<<");
};
}