SpringBoot返回前端Long类型字段丢失精度问题及解决方案

  @Slf4j

  @Configuration

  public class JacksonConfig {

  @Bean

  public Jackson2ObjectMapperBuilderCustomizer customizer() {

  return builder -> {

  // 全局配置序列化返回 JSON 处理

  JavaTimeModule javaTimeModule = new JavaTimeModule();

  javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);

  javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);

  javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);

  javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);

  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

  javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));

  javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));

  builder.modules(javaTimeModule);

  builder.timeZone(TimeZone.getDefault());

  log.info("初始化 jackson 配置");

  };

  }

  }