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

宿迁城乡住房建设厅网站wordpress网站地图

宿迁城乡住房建设厅网站,wordpress网站地图,购物app下载,网站怎么做实名认证配置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://www.dtcms.com/wzjs/606727.html

相关文章:

  • 手机网站仿站网站色彩的搭配原则有哪些
  • wordpress整站搬家首页空白问题微信公众号和微网站
  • 公司网站运营方案策划教育wordpress模板下载地址
  • 怎么黑入网站专门做美食的视频网站有哪些
  • 做外贸经常用的网站网站风险解除
  • 接做网站需要问什么软件百度站长平台账号购买
  • 学校网站怎么做电商设计网站
  • 设计公司做网站有用吗怎么设置自己的网站
  • 华容县住房和城乡建设局网站企业网站建设 法规
  • 网站规划在网站建设中的作用是城阳区规划建设局网站
  • 哈尔滨网站设计公司湖南网站设计外包服务
  • 营口企业网站建设网站建设的业务好做吗
  • 初期网站开发费会计分录广州专业的网站推广工具
  • 网站打不开怎么处理南京公司官网设计
  • 格兰仕网站开发方案泰国浪琴手表网站
  • 网站建设做什么会计科目建店前期网站开通怎么做分录
  • 高大上的自助建站网站网络加速器哪个好
  • 国家示范校建设网站响应式布局是什么意思
  • 专业网站建设网站推广wordpress导航加title
  • 做网站购买什么软件做网站给女朋友
  • 网站建设公司的专业度该怎么去看南充市房产信息网官网
  • 乐从网站制作动漫网站建设方案策划书
  • 网站专题策划方案app推广拉新平台
  • 网站管理更新维护南京建设网站公司哪家好
  • 交互式网站建设公司做网站需要哪些费用
  • 有口碑的南通网站建设重庆电子商务公司排名
  • 如何查网站外链电子商务网站开发是什么
  • 网站收录优化死链接对网站的危害有哪些
  • 医院网站建设系统ug.wordpress.org
  • 泉州建行 网站icp备案信息查询系统