日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 前端文檔 正文

JSON轉(zhuǎn)Long類型時(shí)精度丟失問題解決

作者:ImisLi 更新時(shí)間: 2024-03-25 前端文檔

這個(gè)問題的原因是js在對(duì)長(zhǎng)整型數(shù)據(jù)進(jìn)行處理時(shí)會(huì)損失精度, 從而id發(fā)生變化。

要想解決這個(gè)問題,也很簡(jiǎn)單,只需要讓js處理的ID數(shù)據(jù)類型為字符串類型即可, 這樣就不會(huì)損失精度了

問題修復(fù)

1、JacksonObjectMapper

reggie-common模塊下創(chuàng)建com.itheima.reggie.common.JacksonObjectMapper,自定義映射規(guī)則


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

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

/**
 * 對(duì)象映射器:基于jackson將Java對(duì)象轉(zhuǎn)為json,或者將json轉(zhuǎn)為Java對(duì)象
 * 將JSON解析為Java對(duì)象的過(guò)程稱為 [從JSON反序列化Java對(duì)象]
 * 從Java對(duì)象生成JSON的過(guò)程稱為 [序列化Java對(duì)象到JSON]
 */
public class JacksonObjectMapper extends ObjectMapper {
    public JacksonObjectMapper() {
        super();
        //收到未知屬性時(shí)不報(bào)異常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

        //反序列化時(shí),屬性不存在的兼容處理
        this.getDeserializationConfig().withoutFeatures(FAIL_ON_UNKNOWN_PROPERTIES);

        //自定義轉(zhuǎn)換規(guī)則
        SimpleModule simpleModule = new SimpleModule()
                .addSerializer(BigInteger.class, ToStringSerializer.instance)//將BigInteger轉(zhuǎn)換為String
                .addSerializer(Long.class, ToStringSerializer.instance);//將Long轉(zhuǎn)換成String
        this.registerModule(simpleModule);
    }
}

或是:將其存入springbean中 即 加一個(gè)?@Component


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.stereotype.Component;

import java.math.BigInteger;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

@Component
public class JacksonObjectMapper extends ObjectMapper {
    public JacksonObjectMapper() {
        super();
        //收到未知屬性時(shí)不報(bào)異常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

        //反序列化時(shí),屬性不存在的兼容處理
        this.getDeserializationConfig().withoutFeatures(FAIL_ON_UNKNOWN_PROPERTIES);

        //自定義轉(zhuǎn)換規(guī)則
        SimpleModule simpleModule = new SimpleModule()
                .addSerializer(BigInteger.class, ToStringSerializer.instance)//將BigInteger轉(zhuǎn)換為String
                .addSerializer(Long.class, ToStringSerializer.instance);//將Long轉(zhuǎn)換成String
        this.registerModule(simpleModule);
    }
}

?

2、WebMvcConfig

com.itheima.reggie.config.ReggieWebMvcConfig中添加下面代碼 ?

 //擴(kuò)展mvc框架的消息轉(zhuǎn)換器
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //創(chuàng)建消息轉(zhuǎn)換器對(duì)象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //設(shè)置對(duì)象轉(zhuǎn)換器,底層使用Jackson將Java對(duì)象轉(zhuǎn)為json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //將上面的消息轉(zhuǎn)換器對(duì)象追加到mvc框架的轉(zhuǎn)換器集合中
        converters.add(0, messageConverter);
    }

完整的WebMvcConfig類的代碼:

package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.interceptors.CheckLoginInterceptors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ReggieWebManageWebConfigurer
 * @Description 后臺(tái)管理的webmvc的配置類
 * @Author LPS
 * @Date 2023/7/31 21:51
 */
@Configuration
public class ReggieWebManageWebConfigurer implements WebMvcConfigurer {

    //登錄攔截器
    @Autowired
    private CheckLoginInterceptors checkLoginInterceptors;
    /**
     * 指定靜態(tài)資源加載位置
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //當(dāng)訪問請(qǐng)求是/backend/**時(shí),去classpath:/backend/尋找對(duì)應(yīng)資源
        // Handler --相當(dāng)-- 前端路徑 ,Locations --相當(dāng)-- 后端加載
        //Resource  資源
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
    }

    /*注冊(cè)攔截器*/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> stringList = new ArrayList<>();
        stringList.add("/employee/login");
        stringList.add("/employee/logout");
        stringList.add("/backend/**");
        stringList.add("/error");
        registry.addInterceptor(checkLoginInterceptors)  // 將攔截器添加到攔截列表
                .addPathPatterns("/**")   // 需要攔截的路徑  這里是攔截所有
                .excludePathPatterns(stringList);  // 需要放行的路徑
    }

    //擴(kuò)展mvc框架的消息轉(zhuǎn)換器
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //創(chuàng)建消息轉(zhuǎn)換器對(duì)象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //設(shè)置對(duì)象轉(zhuǎn)換器,底層使用Jackson將Java對(duì)象轉(zhuǎn)為json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //將上面的消息轉(zhuǎn)換器對(duì)象追加到mvc框架的轉(zhuǎn)換器集合中
        converters.add(0, messageConverter);
    }
}

或是:



import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.interceptors.CheckLoginInterceptors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class ReggieWebManageWebConfigurer implements WebMvcConfigurer {

    @Autowired
    private CheckLoginInterceptors checkLoginInterceptors;

    /**
     * 指定靜態(tài)資源加載位置
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //當(dāng)訪問請(qǐng)求是/backend/**時(shí),去classpath:/backend/尋找對(duì)應(yīng)資源
        // Handler --相當(dāng)-- 前端路徑 ,Locations --相當(dāng)-- 后端加載
        //Resource  資源
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
    }

    /*注冊(cè)攔截器*/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> stringList = new ArrayList<>();
        stringList.add("/employee/login");
        stringList.add("/employee/logout");
        stringList.add("/backend/**");
        stringList.add("/error");
        registry.addInterceptor(checkLoginInterceptors)  // 將攔截器添加到攔截列表
                .addPathPatterns("/**")   // 需要攔截的路徑  這里是攔截所有
                .excludePathPatterns(stringList);  // 需要放行的路徑
    }

    //  自定義數(shù)據(jù)類型轉(zhuǎn)換器
    @Autowired
    private JacksonObjectMapper jacksonObjectMapper;

    //擴(kuò)展mvc框架的消息轉(zhuǎn)換器
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //創(chuàng)建消息轉(zhuǎn)換器對(duì)象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //設(shè)置對(duì)象轉(zhuǎn)換器,底層使用Jackson將Java對(duì)象轉(zhuǎn)為json
        messageConverter.setObjectMapper(jacksonObjectMapper);
        //將上面的消息轉(zhuǎn)換器對(duì)象追加到mvc框架的轉(zhuǎn)換器集合中
        converters.add(0, messageConverter);
    }
}

?

原文鏈接:https://blog.csdn.net/ImisLi/article/details/132156735

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新