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

宿迁城乡住房建设厅网站如何建网站免费

宿迁城乡住房建设厅网站,如何建网站免费,dw建设个人网站步骤,广州网站开发 英诺科技配置Spring Boot中的Jackson序列化 在开发基于Spring Boot的应用程序时,Jackson是默认的JSON序列化和反序列化工具。它提供了强大的功能,可以灵活地处理JSON数据。然而,Jackson的默认行为可能无法完全满足我们的需求。例如,日期格…

配置Spring Boot中的Jackson序列化

在开发基于Spring Boot的应用程序时,Jackson是默认的JSON序列化和反序列化工具。它提供了强大的功能,可以灵活地处理JSON数据。然而,Jackson的默认行为可能无法完全满足我们的需求。例如,日期格式、空值处理、数据精度等问题可能需要自定义配置。本文将详细介绍如何在Spring Boot中配置Jackson,以满足这些需求。

1. 为什么需要自定义Jackson配置?

Jackson的默认行为在大多数情况下是合理的,但在实际开发中,我们可能需要对以下方面进行自定义:

  • 日期格式:默认情况下,Jackson会将日期序列化为时间戳,这可能不符合我们的需求。
  • 空值处理:默认情况下,Jackson会忽略空值,但我们可能需要保留空值。
  • 数据精度:对于BigDecimalBigInteger等类型,直接序列化可能会导致精度问题。
  • 自定义序列化:对于某些复杂类型,我们可能需要自定义序列化逻辑。

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 {@Bean@Primary@ConditionalOnMissingBean(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://DQxyvrgZ.Lggng.cn
http://HDDlQY4Z.Lggng.cn
http://v4xUKF5Q.Lggng.cn
http://JOk68Wso.Lggng.cn
http://FajHg6hC.Lggng.cn
http://ySJIH8HY.Lggng.cn
http://2RyOewJN.Lggng.cn
http://obObC52H.Lggng.cn
http://PjbzMeJB.Lggng.cn
http://kmIioFW7.Lggng.cn
http://fj5y7sJJ.Lggng.cn
http://KJxVmekK.Lggng.cn
http://reDkALz2.Lggng.cn
http://shIq83TA.Lggng.cn
http://A3Y8PWX9.Lggng.cn
http://ASwCqeZy.Lggng.cn
http://Zyb32e1V.Lggng.cn
http://Zrc91hvg.Lggng.cn
http://UBNZrcDf.Lggng.cn
http://Qwz13k82.Lggng.cn
http://sXtAdXI6.Lggng.cn
http://yT57TjFu.Lggng.cn
http://z1yumvA3.Lggng.cn
http://hRdXuorJ.Lggng.cn
http://IwlyZmfC.Lggng.cn
http://IhOvRbAL.Lggng.cn
http://8VxP7YZ5.Lggng.cn
http://RdAeGHkq.Lggng.cn
http://J9c9Wcht.Lggng.cn
http://1SWU2XM0.Lggng.cn
http://www.dtcms.com/wzjs/620054.html

相关文章:

  • 有没有人一起做网站html免费模板下载
  • 范县网站建设费用新增备案网站负责人
  • 个人博客网站建设选题说明wordpress页面模板增加
  • 江汉路做网站的公司如何优化网站导航
  • 网址导航网站建站自动跳转手机网站
  • 电子商务网站建设客户需求调查表网站 备案 注销
  • 网站域名怎样选择wordpress怎么对接公众号
  • 企业网站建设需要做些什么北京网络广播电视台
  • 网站怎么做运营推广如何处理脓包痘痘
  • 知名wordpress架构网站成都广告公司地址
  • 轻量级网站开发可以做微网站的第三方平台
  • 网站设计协议烟台网页制作
  • 网上做效果图网站wordpress主机有什么优
  • 网站规划和网站建设如何从客户网站开发客户
  • 上传网站的三种方法百度搜索引擎网址格式
  • 网站怎么做必须交钱吗网站建设贰金手指下拉
  • 深圳 网站建设培训班瑞安网站网站建设
  • 长沙门户网站建设公司北大青鸟网站开发
  • 手机搭建网站工具电子商务网站开发时间进度表
  • 网站建设技术列表如何制作h5海报
  • 怎么做一个免费的网站商务网站开发实训任务书
  • 邢台网站制作哪里好乐都企业网站建设
  • 建设银行代发工资清单网站威海城乡建设局网站
  • 宁波龙山建设有限公司网站网站怎么做营销
  • tp5企业网站开发视频游戏建模培训
  • 用什么网站可以做微信小程序用什么开发工具
  • 吴桥县网站建设价格中国建站平台
  • 商洛 网站建设商丘网站制作费用
  • 网站推销话术本地企业网站建设模板
  • 做网站怎么跟别人讲价免费的二维码生成软件