当前位置: 首页 > news >正文

网站建设情况调查表wordpress 移动端首页

网站建设情况调查表,wordpress 移动端首页,城乡和住房建设部证书信息网,jsp网站 自动发送邮件1、问题说明 后端数据表定义的id主键是Long类型,一共有20多位。 前端在接收到后端返回的json数据时,Long类型会默认当做数值类型进行处理。但前端处理20多位的数值会造成精度丢失,于是导致前端查询数据出现问题。 测试前端Long类型的代码 …

1、问题说明

后端数据表定义的id主键是Long类型,一共有20多位。 前端在接收到后端返回的json数据时,Long类型会默认当做数值类型进行处理。但前端处理20多位的数值会造成精度丢失,于是导致前端查询数据出现问题。 

  • 测试前端Long类型的代码

  • 实际显示效果

2、解决方法

在SpringBoot项目中,默认使用Jackson 来序列化和反序列化 json数据,针对上面的问题,决定采用自定义对象转换器的方式,在后端将数据转为json数据时,将Long类型的数据,统一转换为String类型,再转为json数据返回,避免前端处理Long类型数据造成精度丢失。

3、具体代码实现

在将controller层的返回值数据转为json数据时,需要经过消息转换器MappingJackson2HttpMessageConverter的处理。因此,只需要在消息转换器中判断如果是Long类型的数据,就转换为String类型,再进行json数据的转换。 在代码中,只需要重写extendMessageConverters()方法,添加自定义的对象转换器即可。

1) 编写JacksonObjectMapper对象转换器

该自定义的对象转换器, 主要指定了在进行json数据序列化及反序列化时, LocalDateTime、LocalDate、LocalTime的处理方式,以及BigInteger和Long类型数据,在序列化时直接转换为字符串。

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;/*** 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象* 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]* 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]*/
public class JacksonObjectMapper extends ObjectMapper {public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";public JacksonObjectMapper() {super();//收到未知属性时不报异常this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);//反序列化时,属性不存在的兼容处理this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);SimpleModule simpleModule = new SimpleModule()// 反序列化时的处理 .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))// 序列化时的处理 // 处理 BigInteger 类型.addSerializer(BigInteger.class, ToStringSerializer.instance)// 处理 Long 类型.addSerializer(Long.class, ToStringSerializer.instance).addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))).addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))).addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));//注册功能模块,添加自定义序列化与反序列化器this.registerModule(simpleModule);}
}

2)在WebMvcConfig中重写方法extendMessageConverters,添加自定义对象转换器


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {/*** 扩展mvc框架的消息转换器* @param converters*/@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {log.info("扩展消息转换器...");//创建消息转换器对象MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();//设置对象转换器,底层使用Jackson将Java对象转为jsonmessageConverter.setObjectMapper(new JacksonObjectMapper());//将上面的消息转换器对象追加到mvc框架的转换器集合中converters.add(0,messageConverter);}
}

4、测试

@RestController
public class TestController {@GetMapping("/test")public Map test(){Map<String, Object> map = new HashMap<>();map.put("name", "张三");map.put("age", 18L);return map;}}
  • 不添加转换器

  • 添加转换器

http://www.15wanjia.com/news/168007.html

相关文章:

  • 一一影视网站源码高端办公室装修效果图
  • 免费建立com网站厦门关键词seo排名网站
  • 南通网站制作哪个好下载百度app最新版到桌面
  • 长沙做网站哪个最好淘宝客网站开发
  • 网站制作设计教程建设网站需要备案吗
  • 自己做的小说网站要交税吗管理咨询师证书
  • 国外人像摄影网站传奇手游新开网站
  • 新建网站功能模块淄博seo
  • wordpress音乐墙网站关键词排名优化价格
  • 闸北网站建设wordpress 文章文件
  • 免费申请论坛网站手机网页打不开是什么原因
  • wordpress容易优化吗seo网络排名优化哪家好
  • html5网页模板代码石家庄优化seo
  • 做百度推广的网站网站开发用什么网站
  • 如何搭建论坛网站wordpress 图片加载
  • 国外photoshop教程网站重庆seo公司怎么样
  • 公司门户网站项目模版网站建设的分类
  • 简历电商网站开发经验介绍做网站卖掉
  • 购书网站开发中国对外贸易公司排名
  • 响应式网站源代码企查查企业官网
  • 网站制作在哪里找建设部建设厅报考网站
  • 成品网站w灬 源码1688网页交互型网站
  • 现在网站一般做多大的开发网站 需求
  • 推荐上海网站建站品牌什么是电商
  • 南宁哪些公司专业做网站有实力高端网站设计地址
  • 惠州模板做网站公众号平台app
  • 做网站要会编程么WordPress显示不出广告
  • 重庆网站开发培训机构网站建设钟振森
  • 大连 找人做网站中山免费建站
  • 做网站容易还是做小程序容易免费追剧