Long type returns front-end precision loss

[1] Returning Long to the front end will cause precision loss

In the "Alibaba Java Development Manual", there is a statute on the return of super large integers at the front and back ends. The specific content is as follows:

insert image description here

【2】Problem recurrence

The backend directly uses postman to test the interface, and there is no problem returning data. But when the front end accesses the interface, it is found that the last two digits of the Long type have been replaced with 0, resulting in an error.
Please add a picture descriptionWhy did this happen?
If the returned value exceeds 2 to the 53rd power, it will be converted into a JS Number, and some values ​​may lose precision at this time.

【3】Solution

(1) If this object is only used in this method, you can directly change the attribute from the Long type to the String type.
(2) If this object is used in many places, you can convert the Long type to the String type during serialization.
(3) You can also add a new attribute of String type, which is specially used to transmit such large integers at the front and back ends.

(1) Change the returned information from Long to String

The first method is relatively simple, directly change Long id; to String id;, which is only applicable to this object and only used in this method, which is relatively limited.

(2) Add annotation @JsonFormat to the attribute

The second method can add annotations to the properties. If you use Jackson, you can add @JsonFormat(shape = JsonFormat.Shape.STRING) or @JsonSerialize(using = ToStringSerializer.class) annotations.

If there are many situations that need to be modified, it is still a bit troublesome to add one by one, so is there any good way?

If you are using Jackson, it has a configuration parameter WRITE_NUMBERS_AS_STRINGS, which can force all numbers to be converted into string output. The method of use is very simple, only need to configure parameters: spring.jackson.generator.write_numbers_as_strings=true, this way The advantage is that it is easy to use and does not need to adjust the code; the disadvantage is that the granularity is too large, and all numbers are converted into strings for output, including the time output in timestamp format.

So is there any other way to convert only the Long type into the String type?

Jackson provides this support and can customize ObjectMapper. The specific code is as follows:

public class JacksonConfiguration {
    
    

    @Bean

    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    
    

        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder

                .serializerByType(Long.class, ToStringSerializer.instance)

                .serializerByType(Long.TYPE, ToStringSerializer.instance);

    }

}

By defining Jackson2ObjectMapperBuilderCustomizer, customize the Jackson2ObjectMapperBuilder object, customize the Long data, and use ToStringSerializer for serialization.

(3) Add redundant attributes

The third method requires one more attribute, such as using String dbScripId to replace the previous id.

Guess you like

Origin blog.csdn.net/weixin_44823875/article/details/130670135