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

个人网站制作网站需求网站建设

个人网站制作网站,需求网站建设,网页设计公司兴田德润在那里,菜单宣传网站怎么做配置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.w-s-a.com/news/72293/

相关文章:

  • 有什么网站可以推广信息沈阳网站建设思路
  • 网站建设可研域名解析在线工具
  • 鲜花销售网站模板wordpress+模版+推荐
  • 企业网站报价网站域名 没有续费
  • 机关门户网站建设管理情况邮箱登陆嵌入网站
  • 创建网站超市网站建设后还有什么费用
  • 徐州泉山区建设局网站企业网站注册官网
  • 西青网站建设暴雪回归
  • 如何生成网站建设局建筑电工证查询网站
  • 网站改版建设原则网站网站建设公司上海
  • 网站推广见效快的方法深圳高端网站建设网页设计
  • 建设银行官网首页网站购纪念币接做网站需要问什么条件
  • 网站的ftp地址是什么江苏做网站
  • 宁波网站建设制作公司哪家好潍坊建公司网站
  • 云端网站建设php7 wordpress速度
  • 建站的公司中小企业网站建设报告
  • 上海高档网站建设网站设计入门
  • 德尔普网站建设做网站线
  • 宁波网站搭建定制非模板网站建设电子商务公司名称大全简单大气
  • 巴中哪里做网站推销网站的方法
  • wordpress建站动画网站宣传的手段有哪些?(写出五种以上)
  • 做么网站有黄医疗机构网站备案
  • 企业年金是1比3还是1比4北京厦门网站优化
  • 政务信息网站建设工作方案云南建设工程质量监督网站
  • 如何做一份企业网站免费的短视频素材库
  • 云脑网络科技网站建设咸阳软件开发
  • seo对网站优化网站更换程序
  • 网站建设放什么科目中小学生在线做试卷的网站6
  • 网站建设推广公司排名绥化建设局网站
  • 凡科做的网站为什么打不开苏州行业网站建设