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); } }
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>
时间: 2024-10-11 05:28:40