支持Long超过一定长度自动转字符串,解决js精度丢失问题
This commit is contained in:
40
src/main/java/com/cool/core/config/BigNumberSerializer.java
Normal file
40
src/main/java/com/cool/core/config/BigNumberSerializer.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.cool.core.config;
|
||||
|
||||
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 最大最小值 处理
|
||||
*/
|
||||
@JacksonStdImpl
|
||||
public class BigNumberSerializer extends NumberSerializer {
|
||||
|
||||
/**
|
||||
* 根据 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
28
src/main/java/com/cool/core/config/JacksonConfig.java
Normal file
28
src/main/java/com/cool/core/config/JacksonConfig.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.cool.core.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
@Configuration
|
||||
public class JacksonConfig {
|
||||
|
||||
@Bean
|
||||
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
|
||||
final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
|
||||
final ObjectMapper objectMapper = builder.build();
|
||||
SimpleModule simpleModule = new SimpleModule();
|
||||
// Long,BigInteger 转为 String 防止 js 丢失精度
|
||||
simpleModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
|
||||
simpleModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
|
||||
simpleModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
|
||||
objectMapper.registerModule(simpleModule);
|
||||
|
||||
return new MappingJackson2HttpMessageConverter(objectMapper);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user