小知识-Long.class、Long.TYPE、long.class
文章目录
一、问题描述
后端Long数据序列化到前端,由于数值过大导致数值精度缺失(17位数值最后一位被改为0)
二、解决办法
@Configuration
public class JacksonConfig {
/**
* Jackson全局转化long类型为String,解决jackson序列化时传入前端Long类型缺失精度问题
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
Jackson2ObjectMapperBuilderCustomizer cunstomizer = new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.serializerByType(BigInteger.class, ToStringSerializer.instance);
jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance);
jacksonObjectMapperBuilder.serializerByType(Long.TYPE, ToStringSerializer.instance);
}
};
return cunstomizer;
}
}
三、Long.class、Long.TYPE、long.clas的区别
1.测试代码
public static void main(String[] args) {
System.out.println(Long.class == Long.TYPE);
System.out.println(Long.class == long.class);
System.out.println(Long.TYPE == long.class);
}
2.执行结果
false
false
true
3.源代码
java.lang.Long#TYPE
/**
* The {@code Class} instance representing the primitive type
* {@code long}.
*
* @since JDK1.1
*/
@SuppressWarnings("unchecked")
public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long");
/*
* Return the Virtual Machine's Class object for the named
* primitive type.
*/
static native Class<?> getPrimitiveClass(String name);