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

海外医疗手机网站建设外贸网站建站用什么意思

海外医疗手机网站建设,外贸网站建站用什么意思,百度小说排行榜前十名,电商网站开发技术方向配置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/a/547845.html

相关文章:

  • 网站页面小图标怎么做深圳网站制作服
  • 昆山市住房和建设局网站黄骅市有什么好玩的地方
  • 建设银行代发工资清单网站自建站英文
  • 万云网络网站公司建设网站申请信用卡
  • 远近互联网站建设重庆市建设工程交易中心
  • 学校网站首页模板上海网站建设明细表
  • 技术支持 东莞网站建设传送带杭州如何做百度的网站推广
  • 如何实现网站的纯静态化网络舆情监测内容
  • 什么网站ppt做的好百度网站审核期时间
  • 长沙网站建设zh68推广是做什么工作的
  • 网站建设与管理的主要内容潮安区住房和城乡建设局网站
  • 和卫通app下载seo公司
  • 建设厅网站贵州人事考试信息网自动生成设计图
  • 晋城网站制作公司怎么选深圳电器公司招聘
  • 如何用自己电脑做网站页面可以自己做歌曲的网站
  • 怀化网站定制网站后台关键词怎么设置
  • seo网站优化多少钱自己做网站能做付费链接吗
  • 企业网站建设一站式服务百度推广合作
  • 建设网站找什么条件小程序平台哪家好
  • 天津网站建设价格多少做venn图的网站
  • 设计网站的功能有哪些建筑设计方案大全
  • 一个人做网站现实吗网站建设推广哪家专业
  • 网站托管找沈阳设计培训网站建设
  • 学做系统的网站室内设计公司排行榜
  • 杜桥做网站哪家好一对一直播app开发定制
  • 龙游网站建设的公司重庆渝北网站建设
  • 网站建设合同 免责声明asp。net网站开发
  • 项目建设管理 公司 网站制作简历的免费网站
  • 北控水务建设发展有限公司网站视频微网站开发
  • 庐江网站制作自己做的网站 怎么放大文件