记在前后端分离遇到的一次坑
我使用的是雪花生成器生成的主键id,我的id采用Long类型存储的,
在查询的时候给前台返回的id由于是long类型前端采用number接
收但是长度又不够每次都把最后两位数字转换为00,解决办法是将
Long类型的数据以字符串的方式返回给前端
我返回json的时候采用的是fastjson这里就还是用fastjson配置了转换规则
贴代码如下:
package com.mengxiangnongfu.configure; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.ToStringSerializer; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; /** * @author Yang * @version 1.0 * @date 2020/11/15 21:15 */ @Configuration public class MessageConverters { @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); SerializeConfig serializeConfig = SerializeConfig.globalInstance;
//这里配置 serializeConfig.put(Long.class, ToStringSerializer.instance); serializeConfig.put(Long.TYPE, ToStringSerializer.instance); fastJsonConfig.setSerializeConfig(serializeConfig); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } }