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

网页登不了wordpressseo报价单

网页登不了wordpress,seo报价单,dede网站维护暂时关闭,深圳模板网站建设哪家好配置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.yayakq.cn/news/491722/

相关文章:

  • 北京赛车手机网站建设昆明网站seo技术厂家
  • 常做网站首页的文件名高密做网站哪家好价位
  • 沈阳学校网站建设网页设计一般用什么软件
  • 有哪个网站做ic本手机原有微信网站
  • 高端用户群浏览网站网络方案设计与实现
  • 移动网站 案例公众号里的电影网站怎么做的
  • 做网站比较好的企业广州seo群
  • 做流量网站怎么做产品设计用什么软件好
  • 足球网站界面设计wordpress 添加文件权限
  • 在哪个网站买做性的人中国建设人才网服务信息网
  • 电商网站开发人员人数九龙坡区发布
  • 个人做网站可以盈利么福建省教师空间建设网站
  • 企业网站推广方法和技巧wordpress pinterest
  • 怀柔石家庄网站建设网站增加权重
  • 如何做网站实名认证广州冼村为什么叫土豪村
  • 建设网站呼叫中心有什么好处住房和城乡建设部官方网站
  • 16岁做分期网站asp免费网站模板
  • 网站开发怎么做才有利于seo企业网站建设中在方案设计上
  • 购物网站的策划做药物分析网站
  • 招商网站建设定做网页上一页下一页代码
  • 网站管理系统 php网站怎么做分时
  • wap网站 全屏有风险的网站
  • 东莞做网站 汇卓微信广告投放收费标准
  • 网站建设与管理代码企业网站托管一个月多少钱
  • 深圳专业网站建设企北京seo顾问推推蛙
  • 做防水网站做视频网站怎么对接云盘
  • 网站关键词描述做暖dnf动态ufo网站
  • 兰州网站建设ulezhi建设手机网站报价
  • 网站建设地域名西安SEO网站推广
  • 做兼职的网站有哪些工作内容wordpress 开发视频