The solution to the loss of precision when the front-end js passes in the Long type

Table of contents

problem background

 resolution process

1. Compare database data

2. Query information

Solution


problem background

During business development, it was found that updating a piece of data in the database was unsuccessful. Checking the SQL log found that the SQL was executed normally without error messages, but the number of affected rows was 0, but the data was transmitted from the front end, so there must be this data exists, so why are there 0 items affected?

 resolution process

1. Compare database data

When we went to the database to find the data with id 1597786380514103300, we found that there was no such data, and then we found a data based on other information of this data, and found that the last two digits of the id sent by the front end were inconsistent with the data  

The figure below shows the data id in the database and the id displayed on the front end

 

 Through the comparison of the two ids, we roughly guess that there must be a problem of data loss precision in a certain link of id display from the back end to the front end.

2. Query information

By querying the data, it is found that the maximum length of the Number type of js [JavaScript] is 17 digits. When the received data exceeds 17 digits, it will be rounded up and displayed.

Solution

1. About some solutions browsed from the Internet, the general idea is to convert the Long type to the String type for the front-end display,

start working (solve)

Step 1: Custom Type Converter

/**
 * 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
 * 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
 * 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
 */
public class JacksonObjectMapper extends ObjectMapper {

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public JacksonObjectMapper() {
        super();
        //收到未知属性时不报异常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

        //反序列化时,属性不存在的兼容处理
        this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);


        SimpleModule simpleModule = new SimpleModule()
                //添加序列化器
                .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
                .addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
                .addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))

                .addSerializer(BigInteger.class, ToStringSerializer.instance)
                //将Long类型数据转换为String类型数据
                .addSerializer(Long.class, ToStringSerializer.instance)
                .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
                .addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
                .addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));

        //注册功能模块 例如,可以添加自定义序列化器和反序列化器
        this.registerModule(simpleModule);
    }
}

 Step 2: Replace the MVC default converter

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 功能描述 :扩展消息转换器
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建一个新的消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器,地城使用Jackson将java对象转换为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息类型转换器对象追加到mvc框架的转换器集合中   追加时需要设置我们自定义的索引为0  这样才能优先使用
        converters.add(0, messageConverter);
    }

}

 We have customized a serializer, which can not only convert the Long type to String, but also add serializers for other types of conversion (if you don't need to delete it yourself). This can reduce many small problems due to js conversion.

When running the program, you can see that the default 8 converters of MVC will be loaded at the beginning

Append your own newly defined converter 

 

In this way, the problem of Long losing precision is perfectly solved! ! !

Guess you like

Origin blog.csdn.net/weixin_44693109/article/details/128117451