spring boot 解决前端处理Long溢出问题(转字符串)

思路:针对出参进行序列化处理

复制代码
package com.sikaryofficial.common.security.config;

import com.fasterxml.jackson.databind.MapperFeature;
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.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;


@Configuration
public class JacksonConfig {

    @SuppressWarnings("deprecation")
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        /**
         * 序列化时,将 null 值不输出
         * builder.serializationInclusion(JsonInclude.Include.NON_NULL);
         */
        final ObjectMapper objectMapper = builder.build();
        SimpleModule simpleModule = new SimpleModule();
        // Long 转为 String 防止 js 丢失精度
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        // 忽略 transient 关键词属性
        objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
        return new MappingJackson2HttpMessageConverter(objectMapper);
    }

}

还可以参考如下:

spring boot解决给前端传值,Long型导致JS值溢出的问题,统一转字符串_后端传了个long js泄露了-CSDN博客

相关推荐
无人机90113 分钟前
Delphi 网络编程实战:TIdTCPClient 与 TIdTCPServer 类深度解析
java·开发语言·前端
TeDi TIVE27 分钟前
Spring Cloud Gateway
java
froginwe1130 分钟前
CSS 图像拼合技术
开发语言
计算机安禾39 分钟前
【数据结构与算法】第22篇:线索二叉树(Threaded Binary Tree)
c语言·开发语言·数据结构·学习·算法·链表·visual studio code
:mnong1 小时前
Superpowers 项目设计分析
java·c语言·c++·python·c#·php·skills
a里啊里啊1 小时前
测试开发面试题
开发语言·chrome·python·xpath
豆沙糕1 小时前
Python异步编程从入门到实战:结合RAG流式回答全解析
开发语言·python·面试
信奥胡老师1 小时前
P1255 数楼梯
开发语言·数据结构·c++·学习·算法
A.A呐1 小时前
【C++第二十一章】set与map封装
开发语言·c++
扶苏-su1 小时前
Java--获取 Class 类对象
java·开发语言