开发喵星球

若依微服务处理Long类型精度丢失问题(114)

当字段实体类为Long类型且值超过前端js显示的长度范围时会导致前端回显错误。

1、ruoyi-common-security模块添加JacksonConfig配置全局序列化

package com.ruoyi.common.security.config;

import com.fasterxml.jackson.annotation.JsonInclude;
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;

/**
 * Jackson配置
 */
@Configuration
public class JacksonConfig {

    @SuppressWarnings("deprecation")
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        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);
    }
}

2、增加指定配置类信息

resources\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中新增com.ruoyi.common.security.config.JacksonConfig自动配置类

com.ruoyi.common.security.config.WebMvcConfig
com.ruoyi.common.security.config.JacksonConfig
com.ruoyi.common.security.service.TokenService
com.ruoyi.common.security.aspect.PreAuthorizeAspect
com.ruoyi.common.security.aspect.InnerAuthAspect
com.ruoyi.common.security.handler.GlobalExceptionHandler
   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2024-01-29 14:06:39 阅读量:120
<<   >>


powered by kaifamiao