美文网首页
雪花算法Long类型Id返回前端精度丢失

雪花算法Long类型Id返回前端精度丢失

作者: 走过分叉路 | 来源:发表于2021-05-20 10:33 被阅读0次
    import com.fasterxml.jackson.databind.DeserializationFeature;
    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.json.MappingJackson2HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.*;
    
    import java.text.SimpleDateFormat;
    import java.util.List;
    import java.util.TimeZone;
    
    /**
     * spring mvc 配置
     *
     * @see WebMVCConfiguration
     * @since
     */
    @Configuration
    @EnableWebMvc
    public class WebMVCConfiguration implements WebMvcConfigurer {
        public void configureMessageConverters(List converters) {
            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            SimpleModule simpleModule = new SimpleModule();
    
            simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
    
            simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
    
            objectMapper.registerModule(simpleModule);
    
            // 自定义日期格式化
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            // 设置为东八区的时区
            objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    
            jackson2HttpMessageConverter.setObjectMapper(objectMapper);
    
            converters.add(jackson2HttpMessageConverter);
    
        }
    }
    

    相关文章

      网友评论

          本文标题:雪花算法Long类型Id返回前端精度丢失

          本文链接:https://www.haomeiwen.com/subject/osuvjltx.html