首页 > 代码库 > SpringMVC3.2 解决PO返回前台转成JSON时数据Long精度丢失问题

SpringMVC3.2 解决PO返回前台转成JSON时数据Long精度丢失问题

SpringMVC3.2  解决PO返回前台转成JSON时数据Long精度丢失问题

 

方案

1、自定义一个杰克逊的json转换类

 

技术分享
public class LongToStringAdapter extends ObjectMapper {
    
    
      
    private static final long serialVersionUID = 4402127997078513582L;  
  
    public LongToStringAdapter() {  
        super();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        this.registerModule(simpleModule);
    }
}
View Code

2、配置到springmvc文件中即可

 

技术分享
  <mvc:annotation-driven  conversion-service="conversionService">
        <mvc:message-converters>  
           <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
               <property name="objectMapper">  
                    <!-- <bean class="com.fasterxml.jackson.databind.ObjectMapper">   -->
                    <bean class="mypackage.LongToStringAdapter">
                        <property name="dateFormat">  
                            <bean class="java.text.SimpleDateFormat">  
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                            </bean>  
                        </property>  
                    </bean>   
               </property>
           </bean>
        </mvc:message-converters>  
    </mvc:annotation-driven>
View Code

 

SpringMVC3.2 解决PO返回前台转成JSON时数据Long精度丢失问题