zoukankan      html  css  js  c++  java
  • SpringMVC序列化Long转成String

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

    springmvc.xml

      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
      <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <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 id="customObjectMapper" class="com.nankang.cati.nweb.filter.CustomObjectMapper"/>
    
    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);
        }
    }
    
  • 相关阅读:
    3ds
    markdown-to-html.min.js
    $interpolateProvider
    Angular 插值字符串
    Angular 手动解析表达式
    JAVA 多线程
    listFiles()
    键盘读入
    BufferedInputStream、BufferedOutputStream
    FileInputStream、FileOutputStream的应用
  • 原文地址:https://www.cnblogs.com/sshoub/p/6117391.html
Copyright © 2011-2022 走看看