package com.baiyue.common.web; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; /** * 统一将 Java 长整型序列化为 JSON 字符串,避免浏览器对雪花 ID 产生精度截断。 * *
请求反序列化仍兼容 JSON 字符串和安全范围内的 JSON 数字;数据库与领域模型继续使用 * {@code BIGINT}/{@code Long},本配置只改变 HTTP JSON 表达。
*/ @AutoConfiguration public class LongIdJacksonConfiguration { @Bean Jackson2ObjectMapperBuilderCustomizer longAsStringJacksonCustomizer() { return builder -> builder .serializerByType(Long.class, ToStringSerializer.instance) .serializerByType(Long.TYPE, ToStringSerializer.instance); } }