The back end transmits Long type data to the front end and the precision is lost

Bug site:

When the back end transmits the Long type order number data to the front end, there is a problem of precision loss, as follows

Back end console:

Front end browser:

 

reason:

When js gets the long type returned by the backend, there will be data loss

JS has a built-in 32-bit integer, while the number type security integer is 53 bits. If it exceeds 53 bits, the accuracy will be lost. Just as a 64 bit Long type integer is passed in the background now, so it is over 53 bits, so the value returned in the background will be different from the value obtained in the foreground.

 

 

solve:

Option 1:

Before saving sn to BaseResult, convert it to string (Plus + "")

 

 

Option 2:

You can add in the Long type field of the returned object: @ JsonSerialize(using=ToStringSerializer.class)

Fastjson provides annotations for various data type conversions under the com.alibaba.fastjson.serializer package.

You can extend these annotations by implementing the ObjectSerializer interface,

json transformation needs to use fastjson transformation

 

ToStringSerializer Code:

public class ToStringSerializer implements ObjectSerializer {

    public static final ToStringSerializer instance = new ToStringSerializer();

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                      int features) throws IOException {
        SerializeWriter out = serializer.out;

        if (object == null) {
            out.writeNull();
            return;
        }

        String strVal = object.toString();
        out.writeString(strVal);
    }

}

 

 

Option 3:

This scheme uses the mvc configuration method, where the converter package is created under the current Controller layer

package converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

public class LongToStringJsonConverter extends ObjectMapper {

    public LongToStringJsonConverter() {
        super();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        registerModule(simpleModule);
    }
}

 

Profile:

<mvc:annotation-driven >
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg index="0" value="utf-8" />
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                        <value>text/plain;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
           <-Date converted->
            <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.jay.jackson.util.CustomObjectMapper">
                        <property name="dateFormat">
                            <bean class="java.text.SimpleDateFormat">
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                            </bean>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

 

 

 

After watching, Congratulations, and know a little bit!!!

The more you know, the more you don't know

~Thank you for reading. Your support is the biggest driving force for my study! Come on, strangers work together and encourage together!!

Tags: Programming JSON Java

Posted on Fri, 15 May 2020 10:40:24 -0400 by jayR