北京海淀国税局网站,图片上传 网站建设教学视频,网站集约化建设管理,网站开发分工在我们使用默认的消息转换器#xff0c;将java的Long类型通过json数据传输到前端JS时#xff0c;会导致Long类型的精度丢失#xff0c;这是因为JS处理Long类型数字只能精确到前16位#xff0c;所以我们可以采用自定义序列化方式将Long类型数据统一转为String字符串#xf… 在我们使用默认的消息转换器将java的Long类型通过json数据传输到前端JS时会导致Long类型的精度丢失这是因为JS处理Long类型数字只能精确到前16位所以我们可以采用自定义序列化方式将Long类型数据统一转为String字符串然后再传输到前端即可解决问题。
定义jackson的对象映射器 package com.app.studypro.common;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 com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;/*** 对象映射器:基于jackson将Java对象转为json或者将json转为Java对象* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]** author Administrator*/
public class JacksonObjectMapper extends ObjectMapper {/*** LocalDate类型*/public static final String DEFAULT_DATE_FORMAT yyyy-MM-dd;/*** LocalDateTime类型*/public static final String DEFAULT_DATE_TIME_FORMAT yyyy-MM-dd HH:mm:ss;/*** LocalTime类型*/public static final String DEFAULT_TIME_FORMAT HH:mm:ss;SuppressWarnings(ResultOfMethodCallIgnored)public JacksonObjectMapper() {super();// 收到未知属性时不报异常this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);// 反序列化时属性不存在的兼容处理this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);// 配置注册序列化程序和反序列化程序的自定义信息SimpleModule simpleModule new SimpleModule()// 时间的反序列化.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))// BigInteger的序列化.addSerializer(BigInteger.class, ToStringSerializer.instance)// Long的序列化.addSerializer(Long.class, ToStringSerializer.instance)// 时间的序列化.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));// 注册功能模块以此可以添加自定义序列化器和反序列化器this.registerModule(simpleModule);}}在Spring mvc的消息转换器中添加jackson的对象映射器 package com.app.studypro.config;import com.app.studypro.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.util.List;/*** Spring mvc的配置设定** author Administrator*/
Slf4j
Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {Overrideprotected void extendMessageConverters(ListHttpMessageConverter? converters) {log.info(扩展消息转换器自定义添加 {} 消息转化器到spring mvc中, JacksonObjectMapper.class);// 创建消息转换器对象MappingJackson2HttpMessageConverter messageConverter new MappingJackson2HttpMessageConverter();// 设置对象转换器底层使用Jackson将Java对象转为jsonmessageConverter.setObjectMapper(new JacksonObjectMapper());// 将上面的消息转换器对象追加到mvc框架的转换器集合中将其放在转换器集合的首个位置converters.add(0, messageConverter);}}