Problem description
becausejs
The maximum and minimum values that can be processed are
private static final long MAX_SAFE_INTEGER = 9007199254740991L; private static final long MIN_SAFE_INTEGER = -9007199254740991L;
So our snowflake id is easily out of this range, so we need to convert it to a string for adaptation
For example: 1692419165819899402 will become 16924191658198000000
Solution
Enter the ginseng
In Spring MVC or Spring Boot, you can use @RequestBody to map a JSON field of string type to a long type field in Java as long as the string content is a valid long value.Spring's Jackson library will automatically handle this conversion. Care is necessary to handle possible transformation exceptions to ensure the robustness of the application.
Return to configure the processor
Entity class fields can be defined as long type
package ; import ; import .; import .; import .; import ; import .slf4j.Slf4j; import ; import .Jackson2ObjectMapperBuilderCustomizer; import ; import ; import ; import ; import ; import ; import ; import ; /** * jackson configuration * * @author Lion Li */ @Slf4j @Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilderCustomizer customizer() { return builder -> { // Global configuration serialization returns JSON processing JavaTimeModule javaTimeModule = new JavaTimeModule(); (, ); (, ); (, ); (, ); DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss"); (, new LocalDateTimeSerializer(formatter)); (, new LocalDateTimeDeserializer(formatter)); (javaTimeModule); (()); ("Initialize jackson configuration"); }; } }
package ; import ; import ; import ; import ; import ; /** * JS Maximum and Minimum Value exceeded Processing * * @author Lion Li */ @JacksonStdImpl public class BigNumberSerializer extends NumberSerializer { /** * Get it according to JS Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER */ private static final long MAX_SAFE_INTEGER = 9007199254740991L; private static final long MIN_SAFE_INTEGER = -9007199254740991L; /** * Provide examples */ public static final BigNumberSerializer INSTANCE = new BigNumberSerializer(); public BigNumberSerializer(Class<? extends Number> rawType) { super(rawType); } @Override public void serialize(Number value, JsonGenerator gen, SerializerProvider provider) throws IOException { // Out of range Serialized bit string if (() > MIN_SAFE_INTEGER && () < MAX_SAFE_INTEGER) { (value, gen, provider); } else { (()); } } }
This is the end of this article about SpringBoot jackson precision processing problem solving. For more related SpringBoot jackson precision content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!