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

相关推荐
丘山一郎6 小时前
Spring 中的IOC控制反转 和DI依赖注入
java·后端·spring
码视野6 小时前
基于 Spring Boot + Vue3 的【大学英语四六级 (CET-4/6) 作文智能评分与句式润色系统】设计与实现(含PRD/三端高保真源码/大屏)
java·前端·人工智能·spring boot·后端·vue3
MetaLite6 小时前
Spring-AOP自调用为什么失效-AopContext真能解决吗
java·后端·spring
大模型码小白6 小时前
AI 对话流性能调优:万级消息的虚拟滚动落地
java·大数据·前端·javascript·人工智能·算法·机器学习
m0_547486667 小时前
《JavaScript核心原理 》全套PPT课件2026
开发语言·javascript·ecmascript
许彰午7 小时前
14-字段级SM4加密与SM2签名
java·低代码·架构
MC皮蛋侠客7 小时前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
布莱克6058 小时前
软中断和硬中断的区别
开发语言·操作系统
whcyhhh8 小时前
头歌实践教学平台:数据科学与大数据技术导论(十二)
大数据·开发语言·python·数据清洗
许彰午8 小时前
13-describe元数据输出
java·低代码·架构·状态模式