问题:由于JS中Number的精度为16位(最大位17位,第17位精度不准),我们的ID用的Number 18位,传到客户端会丢失最后两位;
解决方式:Long序列化成String,传到客户端;
注意:客户端取到的Long对应类型是String,做判断或者计算时要注意;

springmvc.xml

  <bean />
  <bean >
    <property name="objectMapper" ref="customObjectMapper"/>
    <property name="supportedMediaTypes">
      <list>
        <value>application/json;charset=UTF-8</value>
        <value>text/html;charset=UTF-8</value>
      </list>
    </property>
  </bean>
  <bean />
public class CustomObjectMapper extends ObjectMapper {
    public CustomObjectMapper() {
        super();
        // 设置日期转换yyyy-MM-dd HH:mm:ss
        setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        // 序列换成json时,将所有的long变成string,因为js中得数字类型不能包含所有的java long值
        SimpleModule simpleModule = new SimpleModule("LongModule", new Version(1, 0, 0, ""));
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        registerModule(simpleModule);
    }
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-05
  • 2021-08-28
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-16
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案