详解springboot之jackson的两种配置方式
作者:山高我为峰 时间:2021-11-03 11:01:21
springboot 针对jackson是自动化配置的,如果需要修改,有两种方式:
方式一:通过application.yml
配置属性说明:##
spring.jackson.date-format指定日期格式,比如yyyy-MM-dd HH:mm:ss,或者具体的格式化类的全限定名
spring.jackson.deserialization是否开启Jackson的反序列化
spring.jackson.generator是否开启json的generators.
spring.jackson.joda-date-time-format指定Joda date/time的格式,比如yyyy-MM-ddHH:mm:ss). 如果没有配置的话,dateformat会作为backup
spring.jackson.locale指定json使用的Locale.
spring.jackson.mapper是否开启Jackson通用的特性.
spring.jackson.parser是否开启jackson的parser特性.
spring.jackson.property-naming-strategy指定PropertyNamingStrategy(CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)或者指定PropertyNamingStrategy子类的全限定类名.
spring.jackson.serialization是否开启jackson的序列化.
spring.jackson.serialization-inclusion指定序列化时属性的inclusion方式,具体查看JsonInclude.Include枚举.
spring.jackson.time-zone指定日期格式化时区,比如America/Los_Angeles或者GMT+10.
常用配置:
spring:
jackson:
#日期格式化
date-format: yyyy-MM-dd HH:mm:ss
serialization:
#格式化输出
indent_output: true
#忽略无法转换的对象
fail_on_empty_beans: false
#设置空如何序列化
defaultPropertyInclusion: NON_EMPTY
deserialization:
#允许对象忽略json中不存在的属性
fail_on_unknown_properties: false
parser:
#允许出现特殊字符和转义符
allow_unquoted_control_chars: true
#允许出现单引号
allow_single_quotes: true
方式二:使用重新注入ObjectMapper
** 在配置bean中使用下面的配置 **
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
{
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
// Include.Include.ALWAYS 默认
// Include.NON_DEFAULT 属性为默认值不序列化
// Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
// Include.NON_NULL 属性为NULL 不序列化
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 允许出现特殊字符和转义符
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
// 允许出现单引号
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// 字段保留,将null值转为""
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
{
@Override
public void serialize(Object o, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException
{
jsonGenerator.writeString("");
}
});
return objectMapper;
}
来源:https://www.cnblogs.com/liaojie970/p/9396334.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java毕业设计实战之图片展览馆管理系统的实现
![](https://img.aspxhome.com/file/2023/1/62521_0s.jpg)
Android数据存储几种方式讲解
![](https://img.aspxhome.com/file/2023/7/86747_0s.png)
Java经典算法汇总之选择排序(SelectionSort)
![](https://img.aspxhome.com/file/2023/3/60343_0s.png)
Java中Request请求转发详解
![](https://img.aspxhome.com/file/2023/8/61538_0s.png)
Java去掉数字字符串开头的0三种方法(推荐)
MybatisPlus多表连接查询的问题及解决方案
![](https://img.aspxhome.com/file/2023/8/60348_0s.png)
Java Spring @Lazy延迟注入源码案例详解
![](https://img.aspxhome.com/file/2023/3/61243_0s.jpg)
IDEA(jetbrain通用)使用教程图解
![](https://img.aspxhome.com/file/2023/9/61389_0s.gif)
Spring Boot2.x如何自定义Endpoint
浅析Java编程中枚举类型的定义与使用
Java使用BigDecimal进行运算封装的实际案例
jdbc和mybatis的流式查询使用方法
Android编程之消息机制实例分析
利用maven引入第三方jar包以及打包
![](https://img.aspxhome.com/file/2023/0/59010_0s.jpg)
详解java中String、StringBuilder、StringBuffer的区别
VSCode 搭建 Arm 远程调试环境的步骤详解
![](https://img.aspxhome.com/file/2023/8/104448_0s.png)
Struts2配置文件中使用通配符的方法(三种形式)
一篇文章带你入门Java数据类型
![](https://img.aspxhome.com/file/2023/8/62338_0s.jpg)
Android 消息机制问题总结
![](https://img.aspxhome.com/file/2023/5/84715_0s.jpg)
SpringBoot整合activemq的案例代码
![](https://img.aspxhome.com/file/2023/9/58589_0s.png)