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博客

相关推荐
郝学胜-神的一滴11 分钟前
Effective Python 第52条:用subprocess模块优雅管理子进程
linux·服务器·开发语言·python
valan liya21 分钟前
C++list
开发语言·数据结构·c++·list
6***x54532 分钟前
Java设计模式之策略模式
java·设计模式·策略模式
Le1Yu33 分钟前
订单取消功能(退款功能、策略模式、定时任务)
开发语言
章鱼哥73036 分钟前
Java 策略模式 + 聚合对象:实现多模块的统计与聚合,快速扩展的实战
java·开发语言·策略模式
是店小二呀40 分钟前
openGauss进阶:使用DBeaver可视化管理与实战
开发语言·人工智能·yolo
万粉变现经纪人42 分钟前
如何解决 pip install 编译报错 ‘cl.exe’ not found(缺少 VS C++ 工具集)问题
开发语言·c++·人工智能·python·pycharm·bug·pip
U***e631 小时前
JavaScript数据分析
开发语言·javascript·数据分析
h***59331 小时前
SpringBoot中如何手动开启事务
java·spring boot·spring
倚肆1 小时前
Java泛型详解:尖括号<>、通配符?与类型参数T
java