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

永久域名免费注册网站重庆市建设工程信息网官网专家评审

永久域名免费注册网站,重庆市建设工程信息网官网专家评审,17网站一起做网店好不好,宁波市网站建设公司配置Spring Boot中的Jackson序列化 在开发基于Spring Boot的应用程序时#xff0c;Jackson是默认的JSON序列化和反序列化工具。它提供了强大的功能#xff0c;可以灵活地处理JSON数据。然而#xff0c;Jackson的默认行为可能无法完全满足我们的需求。例如#xff0c;日期格…配置Spring Boot中的Jackson序列化 在开发基于Spring Boot的应用程序时Jackson是默认的JSON序列化和反序列化工具。它提供了强大的功能可以灵活地处理JSON数据。然而Jackson的默认行为可能无法完全满足我们的需求。例如日期格式、空值处理、数据精度等问题可能需要自定义配置。本文将详细介绍如何在Spring Boot中配置Jackson以满足这些需求。 1. 为什么需要自定义Jackson配置 Jackson的默认行为在大多数情况下是合理的但在实际开发中我们可能需要对以下方面进行自定义 日期格式默认情况下Jackson会将日期序列化为时间戳这可能不符合我们的需求。空值处理默认情况下Jackson会忽略空值但我们可能需要保留空值。数据精度对于BigDecimal和BigInteger等类型直接序列化可能会导致精度问题。自定义序列化对于某些复杂类型我们可能需要自定义序列化逻辑。 2. 配置JacksonConfig 在Spring Boot中可以通过创建一个Configuration类并定义一个ObjectMapper的Bean来自定义Jackson的行为。 import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 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 com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;import java.math.BigDecimal; import java.math.BigInteger; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.TimeZone;/*** author XiaoXin*/ Configuration public class JacksonConfig {BeanPrimaryConditionalOnMissingBean(ObjectMapper.class)public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {builder.simpleDateFormat(yyyy-MM-dd HH:mm:ss);ObjectMapper objectMapper builder.createXmlMapper(false).featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).timeZone(TimeZone.getTimeZone(Asia/Shanghai)).build();// null数据返回objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 反序列化时候遇到不匹配的属性并不抛出异常objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);// 序列化时候遇到空对象不抛出异常objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);// 反序列化的时候如果是无效子类型,不抛出异常objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);// 不使用默认的dateTime进行序列化,objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);// 数据精度问题SimpleModule simpleModule new SimpleModule().addSerializer(Long.class, ToStringSerializer.instance).addSerializer(Long.TYPE, ToStringSerializer.instance).addSerializer(BigInteger.class, ToStringSerializer.instance).addSerializer(BigDecimal.class, ToStringSerializer.instance);objectMapper.registerModule(simpleModule);// 配置Java 8时间日期模块JavaTimeModule javaTimeModule new JavaTimeModule();javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss)));javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(yyyy-MM-dd)));javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(HH:mm:ss)));javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss)));javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(yyyy-MM-dd)));javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(HH:mm:ss)));objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule());return objectMapper;} }
http://www.dnsts.com.cn/news/129820.html

相关文章:

  • 怎么做建设网站首页南昌网站建设大全
  • 中国建设网站工程承包分包法酒店如何做好线上营销
  • 外贸公司没网站旅游网页设计说明书
  • 网软志成免费部队网站源码下载网站建设服务费合同模板
  • 阿里云上如何用iis做网站德州市平原县建设局网站
  • python企业网站开发大桥外语官方网站星做宝贝
  • 编程猫少儿编程网站南宁市规划建设局 网站
  • 网站网页建设论文老师用什么网站做ppt
  • 欧美做的爱爱网站有哪些上海做网站品牌公司有哪些
  • 做网站的框架有广西美丽乡村建设网站
  • 微网站O2O平台平台开发网页广告出价平台
  • 知名建筑设计网站网页版微信登录二维码已失效
  • 中国建设银行网站会员登录wordpress服务器版
  • 设计网站的结构时wordpress打开速度慢
  • arial 网站开发是用犀利wordpress分类标题
  • 网站开发中为什么有两个控制层江苏建站速度忿
  • 上海的网站开发公司电话宠物网站模板
  • 节庆时候的网站是怎么做的直播软件大全
  • 好网站建设公司的网站360网址大全电脑版
  • 外贸响应式网站设计建设网站建设什么挣钱
  • 免费培训学校网站源码做网站主页效果图
  • 形象型网站做网站好的公司
  • 网站开发语言分类找建筑网官网
  • 成都哪家公司做网站最好微信小程序游戏充值破解
  • 如何建设国际网站首页前端是做什么的?
  • 公司注册一站式做网站免费的app是什么
  • 做网站购买服务器吗网站建设财务分析
  • 河南网络洛阳网站建设河南网站建设网站建设定制网站建设公司
  • 在线文库网站建设优秀摄影作品欣赏
  • 上海做网站比较有名的公司免费印章在线制作