18位数字 传到前端 171227112703436652 变成了 171227112703436640
很显然是数字太长,精度丢失了
解决方案:
/** * 序列化json时,将所有的long变成string * 因为js中得数字类型不能包含所有的java long值 */
1 将long类型变成string
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter(
new Jackson2ObjectMapperBuilder()
// .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.serializationInclusion(
Include.NON_NULL).serializerByType(Long.class, new JsonLong2StrConverter())
.serializerByType(Long.TYPE, new JsonLong2StrConverter()).build()));
}
public class JsonLong2StrConverter extends StdSerializer<Long> {
public JsonLong2StrConverter() {
super(Long.class);
}
@Override
public void serialize(Long value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeString(value.toString());
}
}