SpringBoot Long類型序列化json數值損失精度-fastJson以及Jackson解決辦法

現象
   項目中使用mybatisPLUS的ID生成器.生成出的ID是long型的(9568189619433466044),經過某個rest接口中返回json數據後,發現瀏覽器解析完變成了9568189619433466000,損失精度;
緣由 
   大體描述:java中得long能表示的範圍比js中number大,也就意味着部分數值在js中存不下(變成不許確的值);詳情參考這裏 http://stackoverflow.com/questions/17320706/javascript-long-integer ;rest接口返回的json字符串中,數值仍是對的.當js對json進行解析並轉成js object的時候,出現了問題.
解決思路
   不用number來保存long值,而是使用string.能夠在客戶端js修改,也能夠在服務端序列化的時候修改
/**
* jackson時的解決方案
* https://blog.csdn.net/Nicolas12/article/details/83342151
*/
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@EnableWebMvc
@Configuration
public class WebConvertConfig extends WebMvcConfigurerAdapter {
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列換成json時,將全部的long變成string
         * 由於js中得數字類型不能包含全部的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
    }
}
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * fastjson 解決Long精度丟失
 * @author dengw
 * @create 2019-06-26 13:30
 */
public class LongToStringSerializer implements ObjectSerializer {
    public static final LongToStringSerializer instance = new LongToStringSerializer();

    @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);
    }
}

//////////////////////////////////////////////////////////////////////////////////////////

    @JSONField(serializeUsing = LongToStringSerializer.class)
    private Long parkId;

    @JSONField(serializeUsing = LongToStringSerializer.class)
    private Long ioOrderId;

    @JSONField(serializeUsing = LongToStringSerializer.class)
    private Long monthlyOrderId;

    @JSONField(serializeUsing = LongToStringSerializer.class)
    private Long accountId;
相關文章
相關標籤/搜索