SpringMVC使用FastJson并解决长数值精度丢失问题
2018-02-03 18:09
483 查看
项目中很多时候都会用到json,常用的有fastjson,Jackson等等这些,有时候为了统一,我们通常就会约定使用某一种。
不管使用哪种,Spring MVC返回个前段Long类型的数据时,js在获取数据时会丢失精度,从而造成数据的不准确,解决方式呢,就是在序列化时,会将Long类型的数据转化为String类型
在使用Spring MVC默认的Jackson时,我们可以这么做:
不管使用哪种,Spring MVC返回个前段Long类型的数据时,js在获取数据时会丢失精度,从而造成数据的不准确,解决方式呢,就是在序列化时,会将Long类型的数据转化为String类型
在使用Spring MVC默认的Jackson时,我们可以这么做:
1、编写FastJsonConfig扩展类
package com.chz.component.basic.configure; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.ToStringSerializer; import com.alibaba.fastjson.support.config.FastJsonConfig; import java.math.BigInteger; public class FastJsonConfigExt extends FastJsonConfig { public FastJsonConfigExt(){ super(); SerializeConfig serializeConfig = SerializeConfig.globalInstance; serializeConfig.put(BigInteger.class, ToStringSerializer.instance); serializeConfig.put(Long.class,ToStringSerializer.instance); serializeConfig.put(Long.TYPE,ToStringSerializer.instance); this.setSerializeConfig(serializeConfig); } }
2、配置SpringMvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy proxy-target-class="true" /> <mvc:annotation-driven /> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/> <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService"/> </bean> <bean id="fastJsonConfigExt" class="com.chz.component.basic.configure.FastJsonConfigExt"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer" ref="webBindingInitializer"/> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="fastJsonConfig" ref="fastJsonConfigExt" /> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> <value>application/xml;charset=UTF-8</value> <value>application/x-www-form-urlencoded</value> </list> </property> </bean> </list> </property> </bean> <context:annotation-config /> <context:component-scan base-package="com.chz.**.controller,com.cxkh.**.controller" /> <!-- 上传拦截,如最大上传值及最小上传值 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="25474565" /> </bean> <mvc:resources mapping="/static/**" location="classpath:static/"/> <mvc:resources mapping="/*.html" location="/"/> </beans>
相关文章推荐
- 详解在springmvc中解决FastJson循环引用的问题
- SpringMVC与fastjson整合并同时解决中文乱码问题
- 关于java中long类型的数据转换json传到前台时丢失精度问题的解决办法
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- 在springmvc中解决FastJson循环引用的问题
- Android 使用fastjson找不到fastjson包问题的解决方法
- Springboot使用FastJson后,接口返回中文乱码的问题解决(两种解决方式)
- springmvc 中使用fastjson 反序列化json,导致时区相差的问题
- 在springmvc中解决FastJson循环引用的问题
- 使用BigDecimal解决double加减乘除丢失精度问题
- 解决Springboot使用FastJson 返回中文乱码的问题
- SpringMVC与fastjson整合并同时解决中文乱码问题
- 解决java数值范围以及float与double精度丢失的问题
- SpringMVC中使用FastJsonHttpMessageConverter时Swagger2失效的解决办法
- springmvc中解决FastJson循环引用的问题
- SpringMVC使用注解@ResponseBody返回json以及中文乱码问题解决
- Java使用BigDecimal解决浮点型运算丢失精度的问题
- 在springmvc中解决FastJson循环引用的问题
- springMVC使用@RequestBody接收Json的过程,解决415的问题ss
- FastJson与Gson转换成json串丢失精度问题